First, the introduction
The Protocol deals with properties and methods that act like abstract classes in Java, and the types in swift implement agreed properties and methods by complying with the protocol. The protocols in Swift are declared using the Protocol keyword. The protocol in Swift also has a very interesting feature, and protocols can be extended to implement some methods and additional functionality.
Define attributes and methods in the agreement
The attributes defined in the protocol only contract names and types, and in the implementation of a specific type, they can be either stored or computed, and the protocol also needs to specify whether the property is readable or readable. The sample code is as follows:
Protocol Myportocol {/
/Definition Instance Properties
//Readable
var name:string{get}
//readable writable
var age:int{set get}
// Readable
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
= }
}
It is important to note that the readable in the protocol is not read-only a property in a protocol is made readable and writable, the property must be read-writable when implemented, but if the protocol is agreed to be readable, this property can be read-only or readable-to see the specific implementation.
The conventions in a protocol can be either an instance method or a type method, as shown in the following example:
Protocol Myportocol {
func logName ()
static func Logclassname ()
}
class Myclass:myportocol {
var Name:string
var age:int
init () {
name = "HS" Age
=
p func logName () {
print (name) c13/>}
static Func Logclassname () {
print (className)
}
}
Similarly, a protocol can be defined on a construction method.
Iii. Characteristics of the agreement
Although there is no implementation of any properties and methods in the protocol, it can still be used as a type, with a wide range of function parameters and return values, examples are as follows:
Protocol Myportocol {
//Define Instance property
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 ()
} with the protocol type as a parameter
Protocol as a type the other application point is in the collection type, and the protocol can act as a collection type for all compliance with this Protocol.
A protocol can inherit like any other type, and the child protocol automatically owns the properties and methods of the parent Protocol Convention. Protocols can also be defined using the Class keyword to define that only classes can be obeyed, as shown in the following example:
Protocol Myportocol {
//Define Instance property
var name:string{get}
var age:int{set get}
var nameandage:string{get}
static var classname:string{get}
func logName ()
static func Logclassname ()
}
//Only classes can inherit this protocol
protocol Mysubportocol:class,myportocol {
}
Since the protocol can be used like any other type, it can also be checked and converted using is,as?,as!, and more usage of is,as can look at Swift's information about type conversions.
The protocol can also define the attributes or methods that are optional, that is, classes that comply with this protocol can or may not implement optional properties and methods, however, an example is declared to be optional, which requires this protocol to be of the @objc type, as follows:
@objc protocol Myportocol {
//Define Instance property
var name:string{get}
var age:int{set get}
var nameandage: String{get}
static var classname:string{get}
func logName ()
//optional implementation
optional static Func Logclassname ()
}
The protocol in Swift also has a very important feature that can be extended to implement properties, methods, and subscripts. This is handy for some common class methods, which are equivalent to all classes that inherit this protocol by default, as shown in the following example:
Protocol Myportocol {
//Define Instance property
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"
}
}