diogocarmo
diogocarmo

Reputation: 970

RACObserve not working

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

Upvotes: 7

Views: 2169

Answers (1)

Yoichi Tagaya
Yoichi Tagaya

Reputation: 4577

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

class MyViewModel: NSObject {
    dynamic var amount: NSDecimalNumber
}

Upvotes: 17

Related Questions