Created
April 15, 2016 16:00
-
-
Save huguesbr/2e9d48f1e28c1bbf051dc29d24f56b36 to your computer and use it in GitHub Desktop.
Sample demo of global computed variable and global willSet, didSet methods
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 | |
var a: Double = 3 | |
var aSquared: Double { | |
get { | |
return pow(a, 2) | |
} | |
set { | |
a = sqrt(newValue) | |
} | |
} | |
a = 5 | |
print(aSquared) | |
a = 6 | |
print(aSquared) | |
aSquared = 4 | |
print(aSquared) | |
print(a) | |
var greeting: String = "Hello" { | |
willSet(newValue) { | |
print("\(greeting), \(newValue)") | |
} | |
didSet { | |
print("\(oldValue), \(greeting)") | |
} | |
} | |
greeting = "Bye" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment