• Swift中的协议


     

    协议(Protocol)

    1、协议可以用来定义方法、属性、下标的声明,协议可以被枚举、结构体、类遵守(多个协议之间用逗号隔开)

    1. protocol Drawable {
    2. func draw()
    3. var x: Int { get set }
    4. var y: Int { get }
    5. subscript(index: Int) -> Int { get set }
    6. }

    2、协议中定义方法时不能有默认参数值

    3、默认情况下,协议中定义的内容必须全部都实现

    协议中的属性

    1、协议中定义属性时必须用var关键字

    2、实现协议时属性权限要不小于协议中定义的属性权限

            协议定义get、set,用var存储属性或get、set计算属性去实现

            协议定义get,用任何属性都可以实现

    1. class Person: Drawable {
    2. var x: Int = 0
    3. let y: Int = 0
    4. func draw() {
    5. print("Person draw")
    6. }
    7. subscript(index: Int) -> Int {
    8. set {}
    9. get { index }
    10. }
    11. }
    1. class Person: Drawable {
    2. var x: Int {
    3. get { 0 }
    4. set {}
    5. }
    6. var y: Int {
    7. get { 0 }
    8. }
    9. func draw() {
    10. print("Person draw")
    11. }
    12. subscript(index: Int) -> Int {
    13. set {}
    14. get { index }
    15. }
    16. }

    static、class

    1、为了保证通用,协议中必须用static定义类型方法、类型属性、类型下标

    1. protocol Drawable {
    2. static func draw()
    3. }
    4. class Person1: Drawable {
    5. class func draw() {
    6. print("Person1 draw")
    7. }
    8. }
    9. class Person2: Drawable {
    10. static func draw() {
    11. print("Person2 draw")
    12. }
    13. }

    mutating

    1、只有将协议中的实例方法标记为mutating

            才允许结构体、枚举的具体实现修改自身内存

            类在实现方法时不用加mutating,枚举、结构体才需要加mutating

    1. protocol Drawable {
    2. mutating func draw()
    3. }
    4. class Size: Drawable {
    5. var width: Int = 0
    6. func draw() {
    7. width = 10
    8. }
    9. }
    10. static Point: Drawable {
    11. var x: Int = 0
    12. mutating func draw() {
    13. x = 10
    14. }
    15. }

    init

    1、协议里面还可以定义初始化器init

            非final类实现时必须加上required

    1. protocol Drawable {
    2. init(x: Int, y: Int)
    3. }
    4. class Point: Drawable {
    5. required init(x: Int, y: Int) {
    6. }
    7. }
    8. final class Size: Drawable {
    9. init(x: Int, y: Int) {
    10. }
    11. }

    2、如果从协议实现的初始化器,刚好是重写了父类的指定初始化器

            那么这个初始化必须同时加required、override

    1. protocol Liveable {
    2. init(age: Int)
    3. }
    4. class Person {
    5. init(age: Int) {}
    6. }
    7. class Student: Person, Liveable {
    8. required override init(age: Int) {
    9. super.init(age: age)
    10. }
    11. }

    init、init?、init!

    1、协议中定义的init?、init!,可以用init、init?、init!去实现

    2、协议中定义的init,可以用init、init!去实现

    1. protocol Liveable {
    2. init()
    3. init?(age: Int)
    4. init!(no: Int)
    5. }
    6. class Person: Liveable {
    7. required init() {}
    8. // required init!() {}
    9. required init?(age: Int) {}
    10. // required init!(age: Int) {}
    11. // required init(age: Int) {}
    12. required init!(no: Int) {}
    13. // required init?(no: Int) {}
    14. // required init(no: Int) {}
    15. }

    协议的继承

    1、一个协议可以继承其他协议

    协议组合

    1、协议组合,可以包含1个类类型(最多1个)

    1. protocol Livable {}
    2. protocol Runnable {}
    3. class Person {}
    4. //接收Person或者其子类的实例
    5. func fn0(obj: Person) {}
    6. //接收遵守Livable协议的实例
    7. func fn1(obj: Livable) {}
    8. //接收同时遵守Livable和Runnable协议的实例
    9. func fn2(obj: Livable & Runnable) {}
    10. //接收同时遵守Livable、Runnable协议,并且是Person或者其子类的实例
    11. func fn3(obj: Person & Livable & Runnable) {}
    1. typealias RealPerson = Person & Livable & Runnable
    2. func fn4(obj: RealPerson) {}

    CaseIterable

    1、让枚举遵守CaseIterable协议,可以实现遍历枚举值

    1. enum Season: CaseIterable {
    2. case spring, summer, autumn, winter
    3. }
    4. let seasons = Season.allCases
    5. print(seasons.count) // 4
    6. for season in seasons {
    7. print(season)
    8. }

    CustomStringConvertible

    1、遵守CustomStringConvertible协议,可以自定义实例的打印字符串

    1. class Person: CustomStringConvertible {
    2. var age: Int
    3. var name: String
    4. init(age: Int, name: String) {
    5. self.age = age
    6. self.name = name
    7. }
    8. var description: String {
    9. "age = \(age), name = \(name)"
    10. }
    11. }
    12. var p = Person(age: 10, name: "Jack")
    13. print(p) // age = 10, name = Jack
  • 相关阅读:
    忙碌生活下的技术适应力:应对新应用学习带来的困扰与挑战
    【算法修炼】二分查找最接近元素(最接近下标)
    ClickHouse复合数据类型
    口味王cookie
    Redis的启动方法
    Top K 问题解决方案
    【yolo】yolov5-seg实例分割标签
    为什么一定要从DevOps走向BizDevOps?
    【操作系统】测试二
    【SA8295P 源码分析 (一)】01 - SA8295P 芯片介绍
  • 原文地址:https://blog.csdn.net/run_in_road/article/details/125980604