为什么属性观察器不能与类一起使用? [英] Why property observer doesn't work with classes?

查看:94
本文介绍了为什么属性观察器不能与类一起使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用游乐场。

我具有以下结构:

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

我从其中声明了一个实例作为属性观察者默认值为 James 名称表示年龄

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)")
    }
}

尝试编辑实例的名称时:

myFoo.name = "John"

我可以在日志中看到:


我的foo是:Foo(名称: John,年龄:33)

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

我的foo是:Foo(名称: James,年龄:33)

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

这是我的期望。但是,当将 Foo 声明为类而不是结构时: class Foo 并运行完全相同的代码,则不会出现任何内容

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.

其背后的原因是什么?

推荐答案

因为一个类是引用类型,而结构是值类型。

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


  • 假设Foo是一类。 myFoo 只是一个参考。说 myFoo.name = John 会改变对象的位置;没有将新值分配给 myFoo ,因此设置观察者没有什么可观察的。

  • 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.

假设Foo是一个结构。这是一种价值类型。它不能在适当位置突变。因此,说 myFoo.name = John 实际上用新的结构替换了变量 myFoo 中的结构;

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.

这也是为什么即使对 myFoo 用<$ c声明了 myFoo.name = John 的原因$ c> let (即对象只是在适当的位置进行了突变),但是您不能使用struct来做到这一点-您必须声明 myFoo var 以便可以替换变量值。

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.

这篇关于为什么属性观察器不能与类一起使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆