cappie013
cappie013

Reputation: 2444

KVO working once in Swift

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")
    }
}

Upvotes: 6

Views: 1240

Answers (1)

Bryan Luby
Bryan Luby

Reputation: 2567

KVO requires dynamic dispatch, so the dynamic modifier needs to be added to the property:

dynamic var lifes = 0

Upvotes: 11

Related Questions