Created
November 18, 2019 21:14
-
-
Save claybridges/98c5bfd0f586ab51e59cf7dd9fbb526d to your computer and use it in GitHub Desktop.
Property Wrapper Question
This file contains 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
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