Search code examples
iosobjective-creactive-cocoa

How to bind with struct in ReactiveCocoa with Objective-C


I will need to change the border color when meet certain condition like so, but I can't find a way to go through the compiler:

RAC(self.addrTextField.layer, borderColor) = [validateAddressSignal map:^ CGColorRef* (NSArray *array) {

    if (array.count) {
        return [UIColor greenColor].CGColor;
    }
    return [UIColor clearColor].CGColor;
}];

Solution

  • The most elegant solution to a similar question asked on Reactive Cocoa's GitHub Issue tracker is the following posted by Erik Price:

    @interface UIView (MyCategoryName)
    - (void)setMyBorderColor:(UIColor *)color;
    @end
    
    @implementation UIView
    - (void)setMyBorderColor:(UIColor *)color
    {
        self.layer.borderColor = color.CGColor;
    }
    @end
    
    // ...
    
    RAC(myTextField, myBorderColor) = mySignalOfUIColors;
    

    Basically, make it easy to bind the color by adding a category to UIView.