In general, follow the official guidelines.
Use guard statements to check if the property has changed in property observers.
Checking whether the property value has actually changed can help avoid potentially expensive work.
guard
statement, avoid doing unnecessary workvar propWithAnObserver: Bool {
didSet {
guard oldValue != propWithAnObserver else {
return
}
expensiveWork(propWithAnObserver)
needsLayout = true
}
}
guard
statement, do unnecessary workvar propWithAnObserver: Bool {
didSet {
expensiveWork(propWithAnObserver)
needsLayout = true
}
}
defer
statement to trigger property observers after the initializer executesSwift does not call property observers when a class is setting its own properties in the initializer. Use a defer
statement to wrap the property setting statements so that the property assignment and property observers are called after the initializer has been called. This can reduce duplicate code in the initializer.
Since the property assignment happens after the initializer with the use of defer
, this cannot be used on non-optional stored properties because non-optional stored properties require an assigned value before the initializer finishes. However, turning a non-optional stored property into an optional property just to avoid duplication is not recommended.
class House {
init(symbol: String?) {
defer {
self.symbol = symbol
}
}
var symbol: String? {
didSet {
guard oldValue != symbol else {
return
}
announceSymbol()
}
}
}
class House {
init(symbol: String?) {
self.symbol = symbol
announceSymbol()
}
var symbol: String? {
didSet {
guard oldValue != symbol else {
return
}
announceSymbol()
}
}
}