Structs and mutating functions
I haven't used structs much. It seems to be a bit odd with requiring didSet
and mutating
.
The biggest blocker for me is that you can't assign anything to the variables inside a closure, as self
is immutable in structs.
Here's a way to essentially have an observer set up, so that you can keep the struct if you didn't want a class.
struct Thing {
var stuff: Int
mutating func update(stuff: Int) {
self.stuff = stuff
}
}
var thing: Thing? {
didSet {
print("Mutating: \(thing?.stuff)")
}
}
thing = Thing(stuff: 0)
thing?.update(stuff: 1)
thing?.update(stuff: 2)