美文网首页
swift3.0之Protocol简化用法

swift3.0之Protocol简化用法

作者: 售前界的不死小强 | 来源:发表于2017-04-12 17:33 被阅读35次

关于Protocol的介绍和常规用法,这里就不再解释了,文档很多。今天这里要间讲个Protocol的简化用法。这里的使用场景就是我要从A 跳转到B,并在A实现B的多个Protocol。

public protocol AProtocol: NSObjectProtocol {

func APrint()

}

public protocol BProtocol: NSObjectProtocol {

func BPrint()

}

一般的常规做法是

weak var  delegateA: AProtocol?

weak var delegateB: BProtocol?

假设有4个,5个,甚至更多个呢.难道要定义N个变量,这里有个简便的做法:

open weak var delegate: (AProtocol & Protocol)?。无论多少个只要在两个protocol之间加& 即可。

如:open weak var delegate: (AProtocol & BProtocol & Protocol)?

而且在A里面要实现delegate里面的所有protocol。

相关文章

网友评论

      本文标题:swift3.0之Protocol简化用法

      本文链接:https://www.haomeiwen.com/subject/orumattx.html