Skip to content

Instantly share code, notes, and snippets.

@claybridges
Created November 18, 2019 21:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save claybridges/98c5bfd0f586ab51e59cf7dd9fbb526d to your computer and use it in GitHub Desktop.
Save claybridges/98c5bfd0f586ab51e59cf7dd9fbb526d to your computer and use it in GitHub Desktop.
Property Wrapper Question
var defaultNum = 1
class MyClass {
var num: Int {
get { _num ?? defaultNum }
set { _num = newValue }
}
private var _num: Int?
}
let c = MyClass()
print("initial \(c.num)") // ✅ == 1
// changing the default changes the value returned
defaultNum = 2
print("dynamic \(c.num)") // ✅ == 2
// once the property is set, uses that value
c.num = 5
print("set \(c.num)") // ✅ == 5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment