original source : https://www.tutorialspoint.com/swift/swift_protocols.htm
Protocols provide a blueprint for Methods, properties and other requirements functionality. It is just described as a methods or properties skeleton instead of implementation. Methods and properties implementation can further be done by defining classes, functions and enumerations. Conformance of a protocol is defined as the methods or properties satisfying the requirements of the protocol.
Syntax
Protocols also follow the similar syntax as that of classes, structures, and enumerations −
protocol SomeProtocol { // protocol definition }
Protocols are declared after the class, structure or enumeration type names. Single and Multiple protocol declarations are also possible. If multiple protocols are defined they have to be separated by commas.
struct SomeStructure: Protocol1, Protocol2 { // structure definition }
When a protocol has to be defined for super class, the protocol name should follow the super class name with a comma.
class SomeClass: SomeSuperclass, Protocol1, Protocol2 { // class definition }
Property and Method Requirements
Protocol is used to specify particular class type property or instance property. It just specifies the type or instance property alone rather than specifying whether it is a stored or computed property. Also, it is used to specify whether the property is ‘gettable’ or ‘settable’.
Property requirements are declared by ‘var’ keyword as property variables. {get set} is used to declare gettable and settable properties after their type declaration. Gettable is mentioned by {get} property after their type declaration.
protocol classa { var marks: Int { get set } var result: Bool { get } func attendance() -> String func markssecured() -> String } protocol classb: classa { var present: Bool { get set } var subject: String { get set } var stname: String { get set } } class classc: classb { var marks = 96 let result = true var present = false var subject = "Swift 4 Protocols" var stname = "Protocols" func attendance() -> String { return "The (stname) has secured 99% attendance" } func markssecured() -> String { return "(stname) has scored (marks)" } } let studdet = classc() studdet.stname = "Swift 4" studdet.marks = 98 studdet.markssecured() print(studdet.marks) print(studdet.result) print(studdet.present) print(studdet.subject) print(studdet.stname)
When we run the above program using playground, we get the following result −
98 true false Swift 4 Protocols Swift 4
Mutating Method Requirements
protocol daysofaweek { mutating func print() } enum days: daysofaweek { case sun, mon, tue, wed, thurs, fri, sat mutating func print() { switch self { case sun: self = sun print("Sunday") case mon: self = mon print("Monday") case tue: self = tue print("Tuesday") case wed: self = wed print("Wednesday") case mon: self = thurs print("Thursday") case tue: self = fri print("Friday") case sat: self = sat print("Saturday") default: print("NO Such Day") } } } var res = days.wed res.print()
When we run the above program using playground, we get the following result −
Wednesday
Initializer Requirements
Swing allows the user to initialize protocols to follow type conformance similar to that of normal initializers.
Syntax
protocol SomeProtocol { init(someParameter: Int) }
For example
protocol tcpprotocol { init(aprot: Int) }
Class Implementations of Protocol Initializer Requirements
Designated or convenience initializer allows the user to initialize a protocol to conform its standard by the reserved ‘required’ keyword.
class SomeClass: SomeProtocol { required init(someParameter: Int) { // initializer implementation statements } } protocol tcpprotocol { init(aprot: Int) } class tcpClass: tcpprotocol { required init(aprot: Int) { } }
Protocol conformance is ensured on all subclasses for explicit or inherited implementation by ‘required’ modifier. (protocol의 initializer는 모든 subclasses에서 해당 initializer가 implemented되어야 하므로 protocol을 수행하는 첫 superclass에서는 required를 사용해야 한다는 내용으로 이해)
When a subclass overrides its super class initialization requirement it is specified by the ‘override’ modifier keyword.
(참고사항 ref : https://theswiftdev.com/2017/10/10/swift-4-init-patterns/
In Swift initializers are not inherited for subclasses by default. If you want to provide the same initializer for a subclass that the parent class already has, you have to use the override keyword. )
protocol tcpprotocol { init(no1: Int) } class mainClass { var no1: Int // local storage init(no1: Int) { self.no1 = no1 // initialization } } class subClass: mainClass, tcpprotocol { var no2: Int init(no1: Int, no2 : Int) { self.no2 = no2 super.init(no1:no1) } // Requires only one parameter for convenient method required override convenience init(no1: Int) { self.init(no1:no1, no2:0) } } let res = mainClass(no1: 20) let print = subClass(no1: 30, no2: 50) print("res is: (res.no1)") print("res is: (print.no1)") print("res is: (print.no2)")
When we run the above program using playground, we get the following result −
res is: 20 res is: 30 res is: 50