KVO working once in Swift

Deadly 提交于 2019-12-21 13:06:55

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!