Swift for iOS Development (14)-Protocol

version

Xcode 11.3.1
Swift 5.1.3

protocol

The protocol specifies the methods and attributes necessary to achieve a specific function.
Classes, structures or enumeration types can all follow the protocol, and provide specific implementations to complete the methods and functions defined by the protocol.

grammar

protocol SomeProtocol {
    
    
    // 这里是协议的定义部分
}

Let custom types follow multiple protocols:

struct SomeStructure: FirstProtocol, AnotherProtocol {
    
    
    // 这里是结构体的定义部分
}

If a class has a parent class, the parent class name should be preceded by the protocol name, separated by commas:

class SomeClass: SomeSuperClass, FirstProtocol, AnotherProtocol {
    
    
    // 这里是类的定义部分
}

Property requirements

The protocol does not specify whether the attribute is a stored attribute or a calculated attribute, it only specifies the name and type of the attribute.
In addition, the protocol specifies whether the attribute is readable or readable and writable.
If the agreement stipulates readable and writable, then the realization of the agreement is also readable and writable; if the agreement stipulates readable, the realization can be written according to actual needs in addition to being readable.
Add {set get} after the type declaration to indicate that the property is readable and writable, and {get} to indicate read-only.

protocol Subject {
    
    
    var name: String {
    
     get }
    var score: Int {
    
     get set }
}

struct Math: Subject {
    
    
    var name: String {
    
     return "Math" }
    var score = 0
}

var math = Math()
//math.name = "Chinese"   // 只读属性,报错
math.score = 100
print(math.name, math.score)
// 打印 Math 100

Method requirements

The protocol may require the types that follow the protocol to implement certain specified instance methods or class methods.

protocol SayHello {
    
    
    func sayHello()
}

class Person: SayHello {
    
    
    var name: String
    init(name: String) {
    
    
        self.name = name
    }
    func sayHello() {
    
    
        print("\(self.name) say hello")
    }
}

let xiaoming = Person(name: "小明")
xiaoming.sayHello()
// 小明 say hello

let xiaohong = Person(name: "小红")
xiaohong.sayHello()
// 小红 say hello

Constructor requirements

You can implement a constructor in a class that conforms to the protocol, whether as a designated constructor or as a convenience constructor, you must mark the constructor implementation with the required modifier:

protocol SayHello {
    
    
    func sayHello()
}

class Person: SayHello {
    
    
    var name: String
    init(name: String) {
    
    
        self.name = name
    }
    func sayHello() {
    
    
        print("\(self.name) say hello")
    }
}

let xiaoming = Person(name: "小明")
xiaoming.sayHello()
// 小明 say hello

let xiaohong = Person(name: "小红")
xiaohong.sayHello()
// 小红 say hello

protocol StudentName {
    
    
    init(studentName: String)
}

class Student: Person, StudentName {
    
    
    required init(studentName: String) {
    
    
        super.init(name: studentName)
    }
}

let xiaojun = Student(studentName: "学生小军")
xiaojun.sayHello()
// 学生小军 say hello

Commission

Delegation is a design pattern that allows classes or structures to delegate some of the functions they are responsible for to instances of other types.
for example:

// 定义一个看门的协议
protocol WatchDoor {
    
    
    func watchDoor()
}

// 人类会看门,所以遵循 WatchDoor 协议
class People: WatchDoor {
    
    

    var name: String
    init(name: String) {
    
    
        self.name = name
    }
    
    func watchDoor() {
    
    
        print("\(self.name)看门")
    }
    
    // 定义一个被委托对象,来代理看门任务 (现在这个被委托对象是谁还不知道)
    var delegate: WatchDoor? = nil
}

// 打南面来了一条狗
class Dog {
    
    
    var name: String
    init(name: String) {
    
    
        self.name = name
    }
}

// 某天这狗的任督二脉被打通,从此具备看门的能力
extension Dog: WatchDoor {
    
    
    func watchDoor() {
    
    
        print("\(self.name)看门")
    }
}

// 本来是人类小康看门
let people = People(name: "小康")
people.watchDoor()
// 看到狗儿旺财来了
let dog = Dog(name: "旺财")
// 然后委托旺财来看门
people.delegate = dog
// 现在是旺财在看门
people.delegate?.watchDoor()

/**
 小康看门
 旺财看门
 */

Guess you like

Origin blog.csdn.net/u012078168/article/details/104638427