Re: How to write better Swift


 

You can implement a property observer instead. Here’s an example from the Swift book:

class StepCounter {
var totalSteps: Int = 0 {
willSet(newTotalSteps) {
print("About to set totalSteps to \(newTotalSteps)")
}
didSet {
if totalSteps > oldValue {
print("Added \(totalSteps - oldValue) steps")
}
}
}
}

—Jens

Join cocoa@apple-dev.groups.io to automatically receive all group messages.