我想写一个弱属性要求的协议.符合它的类必须能够为此属性指定任何类型.此外,我不想指定实际类型,因此它应该是使用某些协议指定的类型.此代码显示了我对非弱属性的想法:
protocol ObjectProtocol: class {
typealias PropertyType
var property: PropertyType {get set}
}
protocol FirstPropertyProtocol: class {}
protocol SecondPropertyProtocol: class {}
class FirstObjectImpl: ObjectProtocol {
var property: FirstPropertyProtocol?
}
class SecondObjectImpl: ObjectProtocol {
var property: SecondPropertyProtocol?
}
Run Code Online (Sandbox Code Playgroud)
它按预期工作.
我试图为弱属性做同样的事情:
protocol ObjectProtocol: class {
typealias WeakPropertyType: AnyObject //must be a class type
weak var weakProperty: WeakPropertyType? {get set}
}
protocol WeakPropertyProtocol: class {}
class ObjectImpl: ObjectProtocol {
weak var weakProperty: WeakPropertyProtocol?
}
Run Code Online (Sandbox Code Playgroud)
我收到了编译错误:
类型'ObjectImpl'不符合协议'ObjectProtocol'
有什么方法可以让我的工作吗?