1. Introduction
The protocol stipulates some properties and methods, which are similar to abstract classes in Java. Types in Swift implement some agreed properties and methods by complying with the protocol. Protocols in Swift are declared using the protocol keyword. The protocol in Swift also has a very interesting feature. The protocol can be extended to implement some methods and additional functions.
2. Define attributes and methods in the protocol
The attributes defined in the protocol only agree on the name and type. In the implementation of the specific type, it can be a stored attribute or a calculated attribute. The protocol also needs to specify whether the attribute is readable or readable and writable. The sample code is as follows:
protocol MyPortocol { //定义实例属性 //可读的 var name:String{get} //可读可写的 var age:Int{set get} //可读的 var nameAndAge:String{get} static var className:String{get} } class MyClass: MyPortocol { var name: String var age: Int var nameAndAge: String{ get{ return "\(name)"+"\(age)" } } static var className: String{ get{ return "MyClass" } } init(){ name = "HS" age = 24 } }
One thing to note is that readable in the protocol is not read-only. The properties in the protocol are agreed to be readable and writable. When implemented, this property must be readable and writable. However, if the protocol is agreed to be readable and writable, then the property must be readable and writable. Readable, this attribute can be read-only or readable and writable, depending on the specific implementation.
The methods agreed in the protocol can be instance methods or type methods. Examples are as follows:
protocol MyPortocol { func logName() static func logClassName() } class MyClass: MyPortocol { var name: String var age: Int init(){ name = "HS" age = 24 } func logName() { print(name) } static func logClassName() { print(className) } }
Similarly, the constructor method can also be defined in the protocol.
3. Characteristics of the Agreement
Although there are no implementations of any attributes and methods in the protocol, it can still be used as a type and is widely used in function parameters and return values. Examples are as follows:
protocol MyPortocol { //定义实例属性 var name:String{get} var age:Int{set get} var nameAndAge:String{get} static var className:String{get} func logName() static func logClassName() } //将协议类型作为参数 func test(param:MyPortocol) { param.logName() }
Another application point of using protocol as type is in collection types. Protocol can be used as all collection types that comply with this protocol.
Protocols can be inherited like other types, and subprotocols will automatically have the properties and methods agreed upon by the parent protocol. The protocol can also be defined through the class keyword. Only classes can comply with it. The example is as follows:
protocol MyPortocol { //定义实例属性 var name:String{get} var age:Int{set get} var nameAndAge:String{get} static var className:String{get} func logName() static func logClassName() } //只有类可以继承此协议 protocol MySubPortocol:class,MyPortocol { }
Since the protocol can be used like other types, of course it can also be checked and converted using is, as?, as!. For more usage of is and as, you can check Swift’s content on type conversion.
Protocols can also define properties or methods in them as optional, that is, classes that comply with this protocol may or may not implement optional properties and methods. However, declaring them optional requires that the protocol be of type @objc , examples are as follows:
@objc protocol MyPortocol { //定义实例属性 var name:String{get} var age:Int{set get} var nameAndAge:String{get} static var className:String{get} func logName() //可选实现 optional static func logClassName() }
The protocol in Swift also has a very important feature, which can be extended to implement properties, methods and subscripts. This is very convenient for methods of some general classes. This is equivalent to all classes inheriting this protocol implementing such methods by default. Examples are as follows:
protocol MyPortocol { //定义实例属性 var name:String{get} var age:Int{set get} var nameAndAge:String{get} static var className:String{get} func logName() static func logClassName() } extension MyPortocol{ var name:String{ return "HS" } }