-
Notifications
You must be signed in to change notification settings - Fork 79
/
PropertyObservers.swift
28 lines (26 loc) · 889 Bytes
/
PropertyObservers.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
// Property Observers
/*
Property observers observe and respond to changes in a property’s
value. Property observers are called every time a property’s value is
set, even if the new value is the same as the property’s current value.
*/
struct MyAwesomeClass {
var awesomeProperty: String {
willSet {
// This will be called just before the property is set.
print("value is \(self.awesomeProperty)")
print("value will be \(newValue)")
}
didSet {
// This will be called just after the property is set.
print("value is \(self.awesomeProperty)")
print("value was \(oldValue)")
}
}
}
let example = MyAwesomeClass(awesomeProperty: "iPhone7")
example.awesomeProperty = "iPhoneX"
// value is iPhone7
// value will be iPhoneX
// value is iPhoneX
// value was iPhone7