Skip to content

Instantly share code, notes, and snippets.

@huguesbr
Created April 15, 2016 16:00
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 huguesbr/2e9d48f1e28c1bbf051dc29d24f56b36 to your computer and use it in GitHub Desktop.
Save huguesbr/2e9d48f1e28c1bbf051dc29d24f56b36 to your computer and use it in GitHub Desktop.
Sample demo of global computed variable and global willSet, didSet methods
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