我观察到那种我不太了解的行为.在操场上执行此代码时:
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中