Created
January 21, 2016 23:05
-
-
Save hanawat/e59df5b50edf6eaa7ce2 to your computer and use it in GitHub Desktop.
Notice can be understood as the change property before or after.
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
struct Product { | |
let cost: Int | |
var profit = 0 | |
var count = 0 | |
init(cost: Int, price: Int) { | |
self.cost = cost | |
self.price = price | |
} | |
var price: Int { | |
willSet { | |
count += 1 | |
profit = newValue - cost | |
} didSet { | |
print("\(oldValue) => \(price)") | |
} | |
} | |
} | |
var π = Product(cost: 100, price: 200) | |
π.price = 400 | |
π.price = 300 | |
π.price = 500 | |
// 3 times change price. Profit is 400. | |
print("\(π.count) times change price. Profit is \(π.profit).") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment