Created
August 7, 2021 07:03
-
-
Save JarvisTheAvenger/2e891d3ab748b7da6ea541b9e1490cd8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
// Property Observers - `willSet`, `didSet` | |
// Property observers let you observe the change in the state or value | |
// of properties and provides single source of truth | |
// 'didSet' and 'willSet' cannot be provided together with a getter | |
// If `set` is provided then it is mandotory to add `get` | |
class User { | |
var name : String { | |
didSet { | |
print("didSet --> \(name)") | |
} | |
willSet { | |
print("willSet --> \(newValue)") | |
} | |
} | |
init(name: String) { | |
self.name = name | |
} | |
} | |
let user = User(name: "jarvis") | |
user.name = "tony" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment