ReactiveCocoa常用语法

Jan 8, 2017

发表于(2015-11-20 11:13:34)
此为青玉伏案 发表,我觉得挺有概括的,基础学不错,RACSubject我在别的一些文章看到说,不要太范用,不过却又是原生和RAC世界的桥梁,大家看着办吧。
`-(void)uppercaseString{
RACSequence sequence = @[@”you”,@”are”,@”beautiful”].rac_sequence;
RACSignal
signal = sequence.signal;

RACSignal *capitalizedSignal = [signal map:^id(id value) {
return [value capitalizedString];
}];

[signal subscribeNext:^(id x) {
NSLog(@”%@”,x);
}];`

`// [capitalizedSignal subscribeNext:^(id x) {
// NSLog(@”capitalizedSignal —— %@”,x);
// }];
//
//
//
// [[[@[@”you”,@”are”,@”beautiful”].rac_sequence signal]
// map:^id(id value) {
// return [value capitalizedString];
// }] subscribeNext:^(id x) {
// NSLog(@”%@”,x);
// }];

}`

`-(void)signalSwitch{
RACSubject google = [RACSubject subject];
RACSubject
baidu = [RACSubject subject];
RACSubject *signalOfSignal = [RACSubject subject];

RACSignal *switchSignal = [signalOfSignal switchToLatest];

[switchSignal subscribeNext:^(id x) {
NSLog(@”%@”,x);
}];

[signalOfSignal sendNext:baidu];
[baidu sendNext:@”baidu”];
[google sendNext:@”google”];

[signalOfSignal sendNext:google];
[baidu sendNext:@”baidu”];
[google sendNext:@”google”];

}`

`-(void)combinaLatest{
RACSubject signalA = [RACSubject subject];
RACSubject
signalB = [RACSubject subject];

[[RACSignal combineLatest:@[signalA,signalB]
reduce:^id(NSString a,NSString b){
return [a stringByAppendingString:b];
}] subscribeNext:^(id x) {
NSLog(@”x:%@”,x);
}];

[signalA sendNext:@”a”];
[signalA sendNext:@”b”];
[signalB sendNext:@”1”];
[signalB sendNext:@”2”];

}`

`-(void)mergeSignal{
RACSubject letters = [RACSubject subject];
RACSubject
numbers = [RACSubject subject];

RACSubject *chinese = [RACSubject subject];

[[RACSignal merge:@[letters,numbers,chinese]] subscribeNext:^(id x) {
NSLog(@”merge : %@”,x);
}];

[letters sendNext:@”aaa”];
[numbers sendNext:@”666”];
[chinese sendNext:@”好好好”];

}`