我怎样才能将默认实现didSet设置为protocol var
我试试,但我有错误
我试试,但我有错误
Extensions may not contain stored properties
protocol MyProtocol { var contact: MyContact? { get set } } extension MyProtocol { var contact: MyContact? { didSet { // some code } } }
解决方法
来自文档:
Extensions can add new computed properties,but they cannot add stored properties,or add property observers to existing properties.
如果要设置联系人的默认值,则必须是计算属性.
extension MyProtocol { var contact: MyContact? { return MyContact() } }