Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ReactiveCocoa rac_valuesForKeyPath doesn't work in Swift

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?

like image 654
mindz_eye Avatar asked Oct 01 '14 11:10

mindz_eye


1 Answers

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"
  }
}
like image 168
chrs Avatar answered Nov 14 '22 22:11

chrs