Reputation: 65
Being a ReactiveCocoa newbie, I'm hoping for some advice with this:
I'm trying to create a dynamic form that contains multiple Field objects parsed from an XML file. Each Field can have muliple validation rules that will run against the Field's NSString *value
param.
For the RAC part of the question-
inside each Field object, I want to bind BOOL completed
to a signal that checks the Field's *value
param against an array of rules. So far I've gotten here with my thinking:
@implementation Field
self = [super init];
if (self) {
RAC(self, completed) = [RACObserve(self, value) filter:^BOOL(NSString *fieldValue) {
NSLog(@"%s::self.completed = %d\n", sel_getName(_cmd), self.completed); // trying to watch the values here, with no luck
NSLog(@"%s::fieldValue = %@\n", sel_getName(_cmd), fieldValue); // same here, I'd like to be able to view the `*value` here but so far no luck
return [self validateCurrentValue]; // currently this method just checks value.length > 5
}];
}
return self;
The *value
param has already been bound to my view model (successfully) and it gets updated each time a textfield changes.
What I'm looking for is a basic example or best-practice, the code above crashes when run so I know I'm missing something fundamental.
Thanks all
Upvotes: 4
Views: 2783
Reputation: 2465
-filter:
is simply passing values from RACObserve(self, value)
through unchanged, but only if the block returns YES
. So that means you're trying to set completed
to values of whatever type value
is. That's Probably Bad®.
But the good news is that you're really close!
Instead of filtering, you want to transform. You want to take every value
and map it to something other thing. Namely whether that value passes validation. To do that, we use -map:
:
RAC(self, completed) = [RACObserve(self, value) map:^(NSString *fieldValue) {
return @([self validateCurrentValue]);
}];
Upvotes: 5