Skip to content

Instantly share code, notes, and snippets.

@JarvisTheAvenger
Created August 7, 2021 07:03
Show Gist options
  • Save JarvisTheAvenger/2e891d3ab748b7da6ea541b9e1490cd8 to your computer and use it in GitHub Desktop.
Save JarvisTheAvenger/2e891d3ab748b7da6ea541b9e1490cd8 to your computer and use it in GitHub Desktop.
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