I'm trying to adopt ReactiveCocoa in my iOS application written in Swift. Unfortunately, it looks like rac_valuesForKeyPath doesn't work as expected. Here's the example:
class Source: NSObject {
var observable: String = "<Original>"
override init() {
super.init()
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, Int64(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), { () -> Void in
self.observable = "<Updated>"
println("Updated value to \(self.observable)");
})
}
}
class Observer: NSObject {
init(source: Source) {
super.init()
source.rac_valuesForKeyPath("observable", observer: self).subscribeNext { (value: AnyObject!) -> Void in
println(">>> Observable value changed: \(value)")
}
}
}
The example produces the following output:
>>> Observable value changed: <Original>
Updated value to <Updated>
This means subcribeNext block wasn't called.
The expected input is:
>>> Observable value changed: <Original>
Updated value to <Updated>
>>> Observable value changed: <Updated>
Any clue how to fix the issue?
The observable needs to be dynamic
I got your sample to work with the following code
class Source: NSObject {
dynamic var string:String = "Initial Value"
override init() {
super.init()
}
}
class Observer: NSObject {
init(source:Source) {
super.init()
source.rac_valuesForKeyPath("string", observer: self).subscribeNext { (newVal:AnyObject!) -> Void in
println(newVal)
}
}
}
class ViewController: UIViewController {
var source:Source!
var obs:Observer!
override func viewDidLoad() {
super.viewDidLoad()
source = Source()
obs = Observer(source: source)
source.string = "Another Value"
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With