我观察到那种我不太了解的行为.在操场上执行此代码时:
protocol Testp { var notes: String { get set } } class Testc: Testp { var notes: String = "x" } class TestContainer { var test: Testp = Testc() { willSet { print("willSet") } didSet { print("didSet") } } } var testContainer = TestContainer() print(testContainer.test.notes) // prints "x" // this triggers a willSet+didSet call on TestContainer's // stored property,even though "test" is not changed in testContainer testContainer.test.notes = "y" print(testContainer.test.notes) // prints "y"
如上所述,即使未设置属性本身,也会调用willSet和didSet块.
另一方面,如果我将协议更改为类型类如下
protocol Testp: class { var notes: String { get set } }
然后结果就像我期望的那样(即没有调用willSet / didSet).
这种行为的原因是什么?
我在XCode 7.3上运行它.
更新:仍在XCode 9.2 / Swift 4中
解决方法
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。