I'm trying to use RxSwift for binding in MVVM.
I have a Enum
:
enum Color : Int {
case Red = 0, Green
}
and class for test
class Test : NSObject {
var color: Color = .Red
dynamic var test: String? {
didSet {
print("didSet \(test)")
}
}
}
And want to observe changes like:
test.rx_observe(Color.self, "color").subscribeNext { (color) -> Void in
print("Observer \(color)")
}.addDisposableTo(bag)
But the program chashes with
*** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<RDProject.Test 0x7ff373513020> addObserver:<RxCocoa.KVOObserver 0x7ff37351a420> forKeyPath:@"color" options:5 context:0x0] was sent to an object that is not KVC-compliant for the "color" property.'
Code for simple String
works:
test.rx_observe(String.self, "test").subscribeNext { string in
print("Observer \(string)")
}.addDisposableTo(bag)
test.test = "1"
test.test = "2"
I found here that to make class inherited not from NSObject
I should make it dynamic
, but I can't make Enum
dynamic.
Is there a way to make Enum
observable?
Fatal error: Could not extract a String from KeyPath Swift.ReferenceWritableKeyPath ...
Could you confirm this still works? – Whose