Created
August 2, 2012 13:53
-
-
Save prabirshrestha/3237267 to your computer and use it in GitHub Desktop.
Reactive Cocoa examples
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
RACEventTrampoline *trampoline = [[RACEventTrampoline alloc] init]; | |
[trampoline setDelegateMethod:@selector(textFieldShouldReturn:)]; | |
Protocol *protocol = @protocol(UITextFieldDelegate); | |
RACDelegateProxy *proxy = [RACDelegateProxy proxyWithProtocol:protocol andDelegator:self.textField1]; | |
[proxy addTrampoline:trampoline]; | |
[self.textField1 setDelegate:(id<UITextFieldDelegate>)proxy]; | |
[trampoline.subject | |
subscribeNext:^(UITextField *x) { | |
[x resignFirstResponder]; | |
NSLog(@"subscribe next %@", x.text); | |
}]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@synthesize firstName = _firstName; | |
@synthesize txtFirstName = _txtFirstName; | |
[RACAbleSelf(self.firstName) subscribeNext:^(id x) { [self firstNameChanged:x]; }]; | |
[self rac_bind:RAC_KEYPATH_SELF(self.firstName) to:self.txtFirstName.rac_textSubscribable]; | |
- (void) firstNameChanged:(id)firstName { | |
NSLog(@"changed: %@", firstName); | |
} | |
or | |
[[[RACAbleSelf(self.firstName) | |
select:^id(NSString *x) { | |
return [x stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]; | |
}] | |
where:^BOOL(NSString *x) { | |
return x.length > 0; | |
}] | |
subscribeNext:^(id x) { [self firstNameChanged:x]; }]; | |
[self rac_bind:RAC_KEYPATH_SELF(self.firstName) to:self.txtFirstName.rac_textSubscribable]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@property (strong, nonatomic) IBOutlet UITextField *txtFirstName; | |
[[[[[[[[[[self.txtFirstName | |
rac_subscribableForControlEvents:UIControlEventEditingChanged] | |
select:^id(UITextField *x) { | |
return x.text; | |
}] | |
where:^BOOL(NSString *x) { | |
return [x stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]].length > 2; | |
}] | |
throttle:.5 /* seconds */] | |
distinctUntilChanged] | |
select:^id(NSString *x) { | |
return [self searchWikipedia:x]; | |
}] | |
merge] | |
select:^id(id x) { | |
return [x objectAtIndex:1]; | |
}] | |
where:^BOOL(NSArray *x) { | |
return x.count > 0; | |
}] | |
subscribeNext:^(id x) { | |
NSLog(@"---"); | |
NSLog(@"x: %@", x); | |
} error:^(NSError *error) { | |
NSLog(@"%@", error); | |
}]; | |
- (RACAsyncSubject*) searchWikipedia:(NSString*) term { | |
NSURL *url = [NSURL URLWithString:[NSString | |
stringWithFormat:@"http://en.wikipedia.org/w/api.php?action=opensearch&format=json&search=%@", term]]; | |
NSURLRequest * request = [NSURLRequest requestWithURL:url]; | |
RACAsyncSubject *subject = [RACAsyncSubject subject]; | |
AFHTTPRequestOperation *operation = | |
[AFJSONRequestOperation JSONRequestOperationWithRequest:request | |
success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) { | |
[subject sendNext:JSON]; | |
[subject sendCompleted]; | |
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) { | |
NSLog(@"fail"); | |
[subject sendError:error]; | |
}]; | |
[operation start]; | |
return subject; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment