KVO runs once in Swift

I am trying to use KVO in Swift, but the "observValueForKeyPath" method is called once.

Here is the gist of my code

I tried using NSNumber instead of Int , add all parameters to addObserver , but the method still calls once when my view loads.

Any idea?

EDIT: It seems I found a workaround using:

 var lifes: Int { willSet { willChangeValueForKey("lifes") } } 
+6
source share
1 answer

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

dynamic var lifes = 0

+10
source

Source: https://habr.com/ru/post/973551/


All Articles