1. 程式人生 > >ReactiveCocoa有關集合類的使用

ReactiveCocoa有關集合類的使用

pro tsig 獲取 acs mod set instance 遍歷 end

Sequences 集合 表示一個不可變的序列值且不能包含空值,

1.實現NSArray的快速遍歷

NSArray *numbers = @[@1, @2, @3, @4, @5, @6];

//通過rac_sequence來獲取Signal

RACSignal *signal = numbers.rac_sequence.signal;

[signal subscribeNext:^(id x) {

//子線程異步請求

NSLog(@"%@", x);

}];

2.實現NSSet的快速遍歷

NSSet *set = [NSSet setWithObjects:@"A", @"B", @"C", @"D", @"E", @"F", nil];

RACSignal *setSignal = set.rac_sequence.signal;

[setSignal subscribeNext:^(id _Nullable x) {

NSLog(@"%@", x);

}];

3.實現NSDictionary的快速遍歷

NSDictionary *dict = @{@"key1":@"Hello", @"key2":@"World"};

RACSignal *dictSignal = dict.rac_sequence.signal;

[dictSignal subscribeNext:^(id x) {

NSLog(@"%@", x);

//可以看到x打印出來的是RACTuple對象 RACTuple 元組,使用RACTupleUnpack進行解包

RACTupleUnpack(NSString *key, NSString *value) = x;

NSLog(@"key = %@, value = %@", key, value);

}];

4.針對項目中經常遇到的結構復雜的數據結構中的相同模型轉成model

使用rac中的map進行型號傳遞時的轉換

@interface Person : NSObject

@property(nonatomic, strong)NSString *name;

@property(nonatomic, assign)NSInteger sex;

+ (instancetype)personWithDict:(NSDictionary *)dict;

@end

NSArray *personArray = @[@{@"name":@"小張", @"sex":@1}, @{@"name":@"小王", @"sex":@2}];

NSArray *resultArray = [[dictSignal2 map:^id(id value) {

return [Person personWithDict:value];

}] toArray];

NSLog(@"resultArray = %@", resultArray);

ReactiveCocoa有關集合類的使用