问题
I'm trying to use KVO in Swift, but the method "observeValueForKeyPath" is called once.
Here's a GIST of my code
I tried to use NSNumber instead of Int, add all options to addObserver, but the method is still call once when my view load.
Any idea ?
EDIT: It seems like I found a temporary solution using:
var lifes: Int {
willSet {
willChangeValueForKey("lifes")
}
}
回答1:
KVO requires dynamic dispatch, so the dynamic modifier needs to be added to the property:
dynamic var lifes = 0
来源:https://stackoverflow.com/questions/25208362/kvo-working-once-in-swift