Why property observer doesn't work with classes? [duplicate]

☆樱花仙子☆ 提交于 2019-12-11 14:56:16

问题


Using Playground.

I have the following structure:

struct Foo {
    var name: String
    var age: Int

    init(name: String, age: Int) {
        self.name = name
        self.age = age
    }
}

I declared an instance from it as property observer with default values as "James" for the name and 33 for the age:

var myFoo = Foo(name: "James", age: 33) {
    willSet {
        print("my foo will be: \(newValue)")
    }
    didSet {
        print("my foo was: \(oldValue)")
    }
}

When trying to edit the name of the instance:

myFoo.name = "John"

I could see on the log:

my foo will be: Foo(name: "John", age: 33)

my foo was: Foo(name: "James", age: 33)

which is my expectation. However, when declaring Foo as a class instead of structure: class Foo and running the exact same code, nothing will appear on the log.

What is the reason behind it?


回答1:


Because a class is a reference type, whereas a struct is a value type.

  • Suppose Foo is a class. myFoo is merely a reference. Saying myFoo.name = "John" mutates the object in place; no new value is assigned to myFoo, so the setter observer has nothing to observe.

  • Suppose Foo is a struct. That's a value type. It cannot be mutated in place. Thus, saying myFoo.name = "John" actually replaces the struct in the variable myFoo with a new struct; that sets the variable and the setter observer fires.


That is also why you can say myFoo.name = "John" for a class even if myFoo was declared with let (i.e. the object is merely mutated in place), but you can't do that with a struct — you have to have declared myFoo with var so that the variable value can be replaced.



来源:https://stackoverflow.com/questions/49806484/why-property-observer-doesnt-work-with-classes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!