Search code examples
iphoneios7objective-c++reactive-cocoa

ReactiveCocoa observe two property


I need to check two values and set conditions based on these two values, return a NS_ENUM value.

From ReactiveCocoa github readme, I find this

RAC(self, createEnabled) = [RACSignal 
    combineLatest:@[ RACObserve(self, password), RACObserve(self, passwordConfirmation) ] 
    reduce:^(NSString *password, NSString *passwordConfirm) {
        return @([passwordConfirm isEqualToString:password]);
    }];

It check two value, the password and passwordConfirm together. I tried to modify it a bit to observe two BOOL property, it shows me "Incompatible block pointer types" error..

RAC(self, showButtonOption) = [RACSignal
                                    combineLatest:@[ RACObserve(self, setting), RACObserve(self, billing) ]
                                    reduce:^(NSSet *setting, NSSet *billing) {
                                        if ([billing containsObject:kBillingExpired]) {
                                            return DialerShowButtonPurchase;
                                        } else if ([setting containsObject:kSettingEnableRecord]) {
                                            return DialerShowButtonRecord;
                                        } else {
                                            return DialerShowButtonCall;
                                        }
                                    }];

I don't know what went wrong and what should be the right syntax to serve the purpose?


Solution

  • Well, let's see what the signature of that method is:

    + (RACSignal *)combineLatest:(id<NSFastEnumeration>)signals
                          reduce:(id ( ^ ) ( ))reduceBlock
    

    You're trying to return an enum value, a primitive, from the reduceBlock -- which must have return type of id.

    This is an annoying but sadly unavoidable aspect of ReactiveCocoa: you need to box. A lot. If you return @(DialerShowButtonPurchase) (etc), you'll actually be returning an NSNumber *, which is an id, so it'll compile.

    The RAC macro will automatically unbox it so that showButtonOption doesn't need to be declared as an NSNumber *.