微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

swift – 在设置x的属性时,将setSet / didSet调用容器的存储属性x

我观察到那种我不太了解的行为.在操场上执行此代码时:

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中

解决方法

这是因为Testp很容易成为类或结构.如果它是一个结构,那么分配testContainer.test.notes =“y”将创建一个全新的Testp实现实例.另一方面,通过制作协议Testp:class,你明确地告诉对笔记的分配不会改变测试.

考虑一个功能

func doSomething(_ container: TestContainer) {
    testContainer.test.notes = "y"
}

你不知道在这函数中如何使用类或结构配置TestContainer.我想迅速的团队认为总是提高意志/ didChange不那么混乱behaIoUr就是这种情况.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐