RAC中有几种常用的集合类
RACTuple
- 元组类,也相当于RAC中的数组
- 常用创建方式
+ (instancetype)tupleWithObjectsFromArray:(NSArray *)array;
+ (instancetype)tupleWithObjects:(id)object, ... NS_REQUIRES_NIL_TERMINATION;
……
- 案例
RACTuple *tuple = [RACTuple tupleWithObjects:@"aaa", @"bbb", @123, nil];
NSString *str = tuple[0];
NSLog(@"%@", str);
RACSequence
- 代替数组字典,用于快速遍历,常用与数组/字典转模型
//定义一个数组
NSArray *arr = @[@"a", @"b", @"c"];
//RAC集合
RACSequence *req = arr.rac_sequence;
//遍历
RACSignal *signal = req.signal;
[signal subscribeNext:^(id _Nullable x) {
NSLog(@"%@", x);
}];
//简写
[arr.rac_sequence.signal subscribeNext:^(id _Nullable x) {
NSLog(@"%@", x);
}];
//字典时,返回的是元组
NSDictionary *dic = @{@"name":@"abc", @"age":@18};
[dic.rac_sequence.signal subscribeNext:^(RACTuple *x) {
//解包RACTuple
RACTupleUnpack(NSString *key, NSString *value) = x;
NSLog(@"%@:%@", key, value);
}];
网友评论