【发布时间】:2016-10-01 13:28:53
【问题描述】:
我观察到了我不太理解的行为。在操场上执行此代码时:
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 中
【问题讨论】:
-
你解决过这个问题吗?看到同样的问题。
-
我很自私地很高兴终于看到其他人最终陷入了导致问题的境地;)抱歉,但不,除了做事不同之外,我从来没有解决过这个问题。
标签: swift swift-protocols