我希望我的一些类(不是全部)符合使用 'Class-Only Protocols'来自文档。我正在做的是
protocol RefreshData: class, ClassA, ClassB
{
func updateController()
}
我得到了错误
non class type 'RefreshData cannot inherit from classA
non class type 'RefreshData cannot inherit from classB
我不确定我是否完全按照文档中的说明进行操作。有人对此有任何想法吗?
最佳答案
Swift 4 允许您组合类型,因此您可以拥有您的协议(protocol),然后创建,例如,类型别名以将其与特定类要求组合。
对于(一个人为的)例子:
typealias PresentableVC = UIViewController & Presentable
对于呈现的代码:
问题是您试图限制特定的类,而 Swift 无法做到这一点(目前无论如何)。您只能限制为类并从其他协议(protocol)继承。您的语法用于协议(protocol)继承,但您试图将其用作类限制。
注意类协议(protocol)的目的是:
Use a class-only protocol when the behavior defined by that protocol’s requirements assumes or requires that a conforming type has reference semantics rather than value semantics.
关于ios - Swift 中的类协议(protocol),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38270635/