简体   繁体   English

我们可以将 willSet 和 didSet 与 getter 和 setter 一起使用吗?

[英]Can we use willSet and didSet with getter and setter?

I was reading about willset and didset of properties in swift I came to know that I can use these with variable having initial value like below:我正在阅读有关 swift 属性的 willset 和 didset 我开始知道我可以将这些与具有初始值的变量一起使用,如下所示:

var property = "name"
{
    willSet
    {
        print("property is about to changed")
    }
    didSet
    {
        if property == oldValue
        {
            print("values are same")
        }
        else
        {
            print("value changed")
        }
    }
}

property = "anothername"

so can I use willget and didset like below:那么我可以像下面这样使用 willget 和 didset 吗:

var property2:String{

    willSet
    {
        print("value is about to change")
    }
    didSet
    {
        print("value is changes")
    }
}

it gives me this error:它给了我这个错误:

non-member observing properties require an initializer
var property2:String{
    ^

so anyone can explain me what is going on here and can I use getter and setter with willset and didset together like:所以任何人都可以向我解释这里发生了什么,我可以将 getter 和 setter 与 willset 和 didset 一起使用,例如:

var property2:String{

    get{return property2}
    set{propert2 = newValue}

    willSet
    {
        print("value is about to change")
    }
    didSet
    {
        print("value is changes")
    }
}

The error that says you lack an initializer can be solved by giving the property a default value like your first piece of code did:可以通过为该属性提供一个默认值来解决缺少初始化程序的错误,就像您的第一段代码所做的那样:

var property2:String = "Some default value"{

    willSet
    {
        print("value is about to change")
    }
    didSet
    {
        print("value is changes")
    }
}

Now I will answer why can't you use property observers on computed properties.现在我将回答为什么你不能在计算属性上使用属性观察器。

Because there is no point.因为没有意义。

For a settable computed property, you already have the setter, so you can write whatever code you want to execute when the value is set in the setter .对于可设置的计算属性,您已经有了 setter,因此您可以编写在 setter 中设置值时要执行的任何代码。 Why do you need an extra willSet or didSet ?为什么需要额外的willSetdidSet And for a get-only computed property, it can't be set so when do you expect willSet and didSet to be executed?而对于一个 get-only 计算属性,它不能被设置,那么你期望willSetdidSet什么时候执行?

Basically, the set block in computed properties already fulfils the purpose of willSet and didSet .基本上,计算属性中的set块已经实现了willSetdidSet的目的。 Everything you write in willSet you can write it in set before you set the value.您在willSet中写入的所有内容都可以在set值之前将其写入 set 中。 Everything you write in didSet you can write in set after you set the value.您在didSet中写入的所有内容都可以在set值后写入set

Also, note that your third code can cause a Stack Overflow since you are accessing property2 inside its own getter and setting it inside its own setter.另外,请注意,您的第三个代码可能会导致堆栈溢出,因为您正在其自己的 getter 中访问property2并将其设置在其自己的 setter 中。

  • First issue (second snippet) :第一个问题(第二个片段):

    The property / member doesn't have an initial value, that's what the error message says, you need to write an initializer or assign an initial value like in the first snippet.属性/成员没有初始值,这就是错误消息所说的,您需要编写一个初始化程序或分配一个初始值,就像在第一个片段中一样。 The error is not related to the observers.该错误与观察者无关。

  • Second issue (third snippet) :第二个问题(第三个片段):

    Property observers in computed properties are not allowed .不允许在计算属性中使用属性观察者 Your example without the observers doesn't work anyway (assuming propert2 is a typo and you mean property2 ).你没有观察者的例子无论如何都不起作用(假设propert2是一个错字,你的意思是property2 )。 The setter will cause an infinite loop because it's calling itself. setter 将导致无限循环,因为它正在调用自己。

From Apple Doc Classes and structures must set all of their stored properties to an appropriate initial value by the time an instance of that class or structure is created.来自 Apple Doc 类和结构必须在创建该类或结构的实例时将其所有存储的属性设置为适当的初始值。 Stored properties cannot be left in an indeterminate state.存储的属性不能处于不确定状态。

so you can solve this by adding ?所以你可以通过添加来解决这个问题? var property2:String?{ var property2:String?{

var property2:String?{

    willSet
    {
        print("value is about to change")
    }
    didSet
    {
        print("value is changes")
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM