프로토콜이란?
https://docs.swift.org/swift-book/LanguageGuide/Protocols.html
Protocols — The Swift Programming Language (Swift 5.2)
Protocols A protocol defines a blueprint of methods, properties, and other requirements that suit a particular task or piece of functionality. The protocol can then be adopted by a class, structure, or enumeration to provide an actual implementation of tho
docs.swift.org
Swift 공식 문서에 정의된 프로토콜의 정의는
'특정작업 혹은 기능들을 구현하기 위한 메소드나 프로퍼티 등 다른 요구사항들의 청사진인다.'라고 정의되어 있다.
와닿지 않는 설명같아서 여러자료들을 찾아봤다.
공통적으로 하는 설명은 프로토콜은 자바의 Interface와 비슷한 일을 한다는 것이었다.
정리를 하자면, 프로토콜은 내가 사용할 것들에 대한 규약, 약속이며 그런 것들의 미리 정의 해놓은 곳?이라는 느낌이 왔다...
그래도 그동안 개발하면서 delegate를 정의할 때, protocol로 정의를 하는 부분들이 생각이 나서 접목 시켜 생각했더니 이해가 되었다.
프로토콜의 사용에 대해 정리해보면,
1. 프로토콜 이름과 함께 정의한다.
protocol SomeProtocolName {
}
2. 정의된 프로토콜은 클래스나, 구조체에 채택되어 사용된다.
이때 주의 할것은 클래스는 먼저 상속받은 다음, 프로토콜을 채택해야한다.
class SomeClassName: FirstClass, SomeProtocolName {
}
그리고 프로토콜은 다중상속이 가능하다.
'iOS' 카테고리의 다른 글
Swift 싱클톤 패턴 (0) | 2020.11.10 |
---|---|
Swift Joined() (0) | 2020.10.19 |
Swift 구조체 vs 클래스 (0) | 2020.04.13 |
Swift 클로저 (0) | 2020.04.06 |
Swift 메모리 관리 (0) | 2020.04.03 |