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?