第一个:RACTuple 元祖类
- RACTuple类似于数组NSArray
//元祖类 :类似于 数组
RACTuple * tuple = [RACTuple tupleWithObjectsFromArray:@[@"111",@"222",@"333"]];
NSString * str = tuple[1];
NSLog(@"%s == %@",__func__,str);
第二个:RACSequence 集合类
- RACSequence:用于代替NSArray,NSDictionary,可以快速的遍历
- 最常见的应用常见:字典转模型
代码一:快速遍历数组
//数组
NSArray * arr = @[@"abc",@"bcd",@123];
//RAC集合类
RACSequence * requence = arr.rac_sequence;
//遍历
RACSignal * signal = requence.signal;
//订阅信号
[signal subscribeNext:^(id _Nullable x) {
NSLog(@"%s == %@",__func__,x);
}];
整合上述代码
//数组
NSArray * arr = @[@"abc",@"bcd",@123];
[arr.rac_sequence.signal subscribeNext:^(id _Nullable x) {
NSLog(@"%s == %@",__func__,x);
}];
输出结果显示
代码二:解析NSDictionary
//字典
NSDictionary * dic = @{@"name":@"lionsom",@"age":@12};
//字典转集合
[dic.rac_sequence.signal subscribeNext:^(RACTuple* x) {
//宏 : 解析元祖
//宏里面的参数 : 传入需要解析出来的变量名称
//右边,放需要解析的元祖
RACTupleUnpack(NSString * key,NSString * value) = x;
NSLog(@"%@:%@",key,value);
if ([key isEqualToString:@"name"]) {
NSLog(@"Vaule == %@",value);
}
}];
显示结果
代码三:JSON数组快速转Model对象
//文件路径
NSString * path = [[NSBundle mainBundle]pathForResource:@"kfc.plist" ofType:nil];
NSArray * dicArr = [NSArray arrayWithContentsOfFile:path];
NSMutableArray * arr = [NSMutableArray array];
[dicArr.rac_sequence.signal subscribeNext:^(id _Nullable x) {
//将对象进行逐个转换,然后添加到对象数组中
KFC * kfc = [KFC kfcWithDict:x];
[arr addObject:kfc];
}];
代码优化:
NSArray * arr_1 = [[dicArr.rac_sequence map:^id _Nullable(id _Nullable value) {
return [KFC kfcWithDict:value];
}] array];
NSLog(@"%@",arr_1);
网友评论