swift进阶(五)协议

协议的用法

  • 语法格式
protocol MyProtocol{
	//body
}
  • class、struct、enum都可以遵守协议,多个协议,使用逗号分隔
struct Person: Protocol1, Protocol2{
//body
}
  • 协议中添加属性

    • 协议同时要求一个属性必须明确可读的或者 可读的和可写的
    • 属性要求是变量属性,不能使用let声明
protocol MyProtocol{
	var age : Int{ get }
	var name : String{get set}
	}
  • 在协议中定义方法,我们只需要定义当前方法的名称、参数列表和返回值
protocol MyProtocol {
    func teach()
    static func teach2()
}

struct Person:MyProtocol {
    func teach() {
        
    }
    
    static func teach2() {
        
    }
}
  • 协议中也可以定义初始化方法,但是必须使用required关键字
protocol MyProtocol {
    init(age:Int)
}

class Person:MyProtocol {
    
    required init(age:Int){
        
    }
}
  • 将协议作为类型

    • 作为函数,方法或者初始化程序中的参数类型或返回类型
    • 作为常量,变量或属性的类型
    • 作为数组,字典或其他容器中项目的类型

```swift
protocol Shape{
    var area: Double{ get }
}

class Circle: Shape{
    var radious: Double

    init(_ radious: Double) {
        self.radious = radious
    }

    var area: Double{
        get{
            return radious * radious * 3.14
        }
    }
}

class Rectangle: Shape{
    var width, height: Double

    init(_ width: Double, _ height: Double) {
        self.width = width
        self.height = height
    }

    var area: Double{
        get{
            return width * height
        }
    }
}

var circle: Shape = Circle.init(10.0)
var rectangle: Shape = Rectangle.init(10.0, 20.0)

var shapes: [Shape] = [circle, rectangle]

for shape in shapes{
    print(shape.area)
}
  • 协议默认实现
protocol MyProtocol {
    func teach(age:Int)
}

extension MyProtocol{
    func teach(age:Int){
        
    }
}

class Person:MyProtocol {
}
  • 思考:看一下代码打印是什么?

protocol MyProtocol {
    func teach()
}

extension MyProtocol {
    func teach() {
        print("MyProtocol")
    }
    
}

class MyClass: MyProtocol {
    func teach() {
        print("MyClass")
    }
}

let object: MyProtocol = MyClass()
object.teach()

let object1: MyClass = MyClass()
object1.teach()
  • 打印输出
    在这里插入图片描述
  • 那么下面应该输出什么呢?
protocol MyProtocol {
}

extension MyProtocol {
    func teach() {
        print("MyProtocol")
    }
    
}

class MyClass: MyProtocol {
    func teach() {
        print("MyClass")
    }
}

let object: MyProtocol = MyClass()
object.teach()

let object1: MyClass = MyClass()
object1.teach()
  • 打印结果
    在这里插入图片描述

  • 上述的两个打印为什么不一样呢?生成sil代码查看一下调用

    • 协议中声明teach方法

在这里插入图片描述
在这里插入图片描述

查看一下witness_method是什么,发现是直接调用的是类的methodList中的teach方法
在这里插入图片描述

  • 协议中没有声明teach方法
    在这里插入图片描述

  • witness_table没有值

在这里插入图片描述

小结

  • witness_method:通过PWT(协议目击表)获取对应的函数地址
  • class_method:该指令通过类的函数表来查找函数,基于类的实际类型。

底层分析

根据下述代码,先查看一下两个变量占用内存的大小

在这里插入图片描述

  • 生成IR代码查看底层逻辑
    在这里插入图片描述
    其中T4main10MyProtocolP是一个结构体
    在这里插入图片描述
  • 转成swift代码是
24字节
struct protocolData {
    var value1: UnsafeRawPointer
    var value2: UnsafeRawPointer
    var value3: UnsafeRawPointer
    var type: UnsafeRawPointer
    var pwt: UnsafeRawPointer
}

在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值