5. You want to create a property that is both clamped between 0 and 100 and logged when changed. You have two property wrappers:
@propertyWrapper
struct Clamped {
private var value: Int
let range: ClosedRange<Int>
var wrappedValue: Int {
get { value }
set { value = min(max(newValue, range.lowerBound), range.upperBound) }
}
init(wrappedValue: Int, _ range: ClosedRange<Int>) {
self.range = range
self.value = min(max(wrappedValue, range.lowerBound), range.upperBound)
}
}
@propertyWrapper
struct Logged {
private var value: Int
var wrappedValue: Int {
get { value }
set {
print("Value changed to \(newValue)")
value = newValue
}
}
init(wrappedValue: Int) {
self.value = wrappedValue
}
}
struct Settings {
@Logged @Clamped(0...100) var score: Int = 150
}What will be printed and what is the final value of
Settings().score?