Skip to content

Instantly share code, notes, and snippets.

@lucianoschillagi
Created September 6, 2024 15:10
Show Gist options
  • Save lucianoschillagi/6ab379e2ebd6815d5c4b01f6571bdceb to your computer and use it in GitHub Desktop.
Save lucianoschillagi/6ab379e2ebd6815d5c4b01f6571bdceb to your computer and use it in GitHub Desktop.
property observers example
import Cocoa
/// Property Observers 👀
class Person {
var favoriteColor = "BLUE" {
// and now my 'favoriteColor' will be...
willSet(newFavoriteColor) {
print("My favorite color NOW is '\(newFavoriteColor)'")
}
// my 'favoriteColor' was...
didSet {
print("My favorite color was '\(oldValue)'")
}
}
}
let changingPerson = Person()
changingPerson.favoriteColor = "RED"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment