7

I'm trying to observe a property in my ViewModel, and then update a label with it's value using ReactiveCocoa, but it's not updating.

Here's what I got:

ViewModel

var amount: NSDecimalNumber

ViewController

RAC(self.amountLabel, "text") <~ RACObserve(self.viewModel, "amount").map({
            (value) -> AnyObject! in
                let numberFormatter = NSNumberFormatter()
                numberFormatter.numberStyle = .CurrencyStyle

                return numberFormatter.stringFromNumber(value as NSDecimalNumber)
        })

I checked and the ViewModel is updating the 'amount' property correctly. Is there anything I'm missing here?

I also tried this for testing:

RACObserve(self.viewModel, "amount").subscribeNext { 
    (value) -> Void in
        println(value)
}

Doesn't work either.

I'm using ReactiveCocoa 2.4.7 because my app is supporting iOS 7. Are there any incompatibilities between the macro replacements in Swift [1,2] and this version?

[1] - https://github.com/ashfurrow/Swift-RAC-Macros

[2] - http://blog.scottlogic.com/2014/07/24/mvvm-reactivecocoa-swift.html

diogocarmo
  • 960
  • 1
  • 11
  • 30
  • 1
    By the way, read Ivan's comment in [the second reference](http://blog.scottlogic.com/2014/07/24/mvvm-reactivecocoa-swift.html). The RACObserve in Swift is different from RACObserve macro in Objective-C, and it can be a problem. – Yoichi Tagaya Mar 04 '15 at 15:29

1 Answers1

17

Mark the property as dynamic, and make sure the view model inherits NSObject.

class MyViewModel: NSObject {
    dynamic var amount: NSDecimalNumber
}
Yoichi Tagaya
  • 4,547
  • 2
  • 27
  • 38