Search code examples
swiftreactive-cocoafrp

Observing sub-class properties in Swift (Reactive Cocoa)


I am new to Reactive Cocoa and to be honest it's not as simple to get into FRP as I thought it would be. Anyway, the concept is awesome and I'm trying to implement RAC in my current Swift project.

The situation:

MainTableViewController sets a property:

var userLocation = LocationManager()

LocationManager saves the location info in a dynamic variable.

dynamic var newLocation = [String]()

Back in MainTableViewController I tried to set up a RACSignal observing the newLocation variable:

rac_valuesForKeyPath("newLocation", observer: self.userLocation).subscribeNextAs { () -> () in
        println("New location received!")
    }

I checked other projects (in Objective-C) where it worked that way. But as soon as I compile the app xCode tells me:

2014-12-08 13:06:17.122 Test[28145:3242245] *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<Test.MainTableViewController 0x14c612570> valueForUndefinedKey:]: this class is not key value coding-compliant for the key newLocation.'

I'd appreciate any kind of help...

Thanks :)


Solution

  • The observation is backwards. Try:

    userLocation.rac_valuesForKeyPath("newLocation", observer: self).subscribeNextAs { (value) -> () in
        println("New location received!")
    }