I think it's nice if the code was interface drive
so how to make ReactiveCocoa work for protocol in swift?
e.g.
protocol SomeBody {
var name: String { get set }
}
...
class XXViewController {
@IBOutlet weak var someLabel:UILabel!
var someOne: SomeBody {
didSet {
RACObserve(someOne, "name").subscribeNext { [weak self](name) -> Void in
self?.someLabel.text = name as! String
}
}
}
....
}
RACObserve
is built on top of the KVO, so for it to work name
needs to be key-value observable. Objects in Swift are not KVO compliant by default.
You can add KVO compliance by inheriting from NSObject
:
class Foo : NSObject, SomeBody {
var name: String = ""
}
Or making individual property observable:
class Foo : SomeBody {
dynamic var name: String = ""
}
AFAIK, there is no way to enforce this behaviour by protocol definition, only support it in the individual implementations.