我正在尝试使用 RxSwift 在 MVVM 中进行绑定。我有一个Enum
:
enum Color : Int {
case Red = 0, Green
}
和考试课
class Test : NSObject {
var color: Color = .Red
dynamic var test: String? {
didSet {
print("didSet \(test)")
}
}
}
并希望观察以下变化:
test.rx_observe(Color.self, "color").subscribeNext { (color) -> Void in
print("Observer \(color)")
}.addDisposableTo(bag)
但程序与
*** 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.'
简单String
作品的代码:
test.rx_observe(String.self, "test").subscribeNext { string in
print("Observer \(string)")
}.addDisposableTo(bag)
test.test = "1"
test.test = "2"
我在这里发现,要让类不是从我那里继承的,NSObject
我应该让它dynamic
,但我不能让它成为Enum
动态的。有没有办法让Enum
observable?