Im new to RCA. I found this tut online ReactiveCocoaSamurai
Its pretty simple...In the original project they separate the model from the view controller, as they should :) But when I try to do it, I can't bind (or connect) the view controller property to the model property.
Everything is set up correctly because if I put all the code in the view controller class, it WORKS! The reason why it works is because I don't need the line of code in the view controller that binds the model to the view controller properties.
But when I try to split it up as the tutorial has it, I get an error at that line.
This works when in the view controller:
The Signal method
-(RACSignal *)forbiddenNameSignal {
return [RACObserve(self, username) filter:^BOOL(NSString *newName) {
return [self.forbiddenNames containsObject:newName]; //This yields a YES BOOLEAN?
}];
}
This goes in the viewDidLoad of the viewController:
[[self.usernameField.rac_textSignal distinctUntilChanged] subscribeNext:^(NSString *x) {self.username = x;}];
[self.mymodel.forbiddenNameSignal subscribeNext:^(NSString *name) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Forbidden Name!"
message:[NSString stringWithFormat:@"The name %@ has been forbidden!",name]
delegate:nil
cancelButtonTitle:@"Ok"
otherButtonTitles:nil];
[alert show];
self.mymodel.username = @"";
}];
Of course the UITextField is in storyboards and hooked up and the forbidden array is defined in viewDidLoad as well.
This doesn't work:
LoginModel.m
-(id)init {
self = [super init];
if(!self) return nil;
//forbidden names array
self.forbiddenNames = @[ @"Peter",@"Piper",@"Picker"];
return self;
}
-(RACSignal *)forbiddenNameSignal {
return [RACObserve(self, username) filter:^BOOL(NSString *newName) {
return [self.forbiddenNames containsObject:newName]; //This yields a YES BOOLEAN?
}];
}
ViewController.m viewDidLoad:
self.mymodel = [LoginModel new];
RAC(self.usernameField.text) = [RACAbleWithStart(self.mymodel.username) distinctUntilChanged];
[[self.usernameField.rac_textSignal distinctUntilChanged] subscribeNext:^(NSString *x) {
self.username = x;
}];
[self.mymodel.forbiddenNameSignal subscribeNext:^(NSString *name) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Forbidden Name!"
message:[NSString stringWithFormat:@"The name %@ has been forbidden!",name]
delegate:nil
cancelButtonTitle:@"Ok"
otherButtonTitles:nil];
[alert show];
self.mymodel.username = @"";
}];
I get an error at the RAC line saying expected identifier & warning of "signalWithStarting..." deprecated for "valuesForKeyPath".
What I have tried:
RAC(self, usernameField) = [RACObserve(self, mymodel.username) distinctUntilChanged];
and ...
RAC(self, usernameField) = RAC(self, mymodel.username);
Any ideas how to fix this?
Or about why indenting code is not working either :(