Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 324
diogocarmo Avatar asked Mar 04 '15 15:03

diogocarmo


1 Answers

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

class MyViewModel: NSObject {
    dynamic var amount: NSDecimalNumber
}
like image 95
Yoichi Tagaya Avatar answered Oct 07 '22 02:10

Yoichi Tagaya