[基础]RACTuple和RACSequence

2016-11-22  本文已影响0人  古愿狼

RACTuple

    //普通创建
    RACTuple *tuple1 = [RACTuple tupleWithObjects:@1, @2, @3, nil];
    RACTuple *tuple2 = [RACTuple tupleWithObjectsFromArray:@[@1, @2, @3]];
    RACTuple *tuple3 = [[RACTuple alloc] init];

    //宏创建
    RACTuple *tuple4 = RACTuplePack(@1, @2, @3, @4);

    //解包(等号前面是参数定义,后面是已存在的Tuple,参数个数需要跟Tuple元素相同)
    RACTupleUnpack(NSNumber * value1, NSNumber * value2, NSNumber * value3, NSNumber * value4) = tuple4;
    NSLog(@"%@ %@ %@ %@", value1, value2, value3, value4);

    //元素访问方式
    NSLog(@"%@", [tuple4 objectAtIndex:1]); 
    NSLog(@"%@", tuple4[1]);

RACSequence

    NSArray *array = @[@1, @2, @3];
    NSDictionary *dict = @{@"key1" : @"value1", @"key2" : @"value2", @"key3" : @"value3"};
    NSString *str = @"ABC";
    NSSet *set = [NSSet setWithArray:array];
    RACTuple *tuple = [RACTuple tupleWithObjectsFromArray:array];

    //NSArray 会返回元素
    [array.rac_sequence.signal subscribeNext:^(id _Nullable x) {
        NSLog(@"array rac_sequence : %@", x);
    }];

    //NSDictionary 会返回打包成Tuple的key、value
    [dict.rac_sequence.signal subscribeNext:^(id _Nullable x) {
        NSLog(@"dict rac_sequence : %@", x);
    }];

    //NSString 会返回单个字符
    [str.rac_sequence.signal subscribeNext:^(id _Nullable x) {
        NSLog(@"str rac_sequence : %@", x);
    }];

    //NSSet 会返回元素
    [set.rac_sequence.signal subscribeNext:^(id _Nullable x) {
        NSLog(@"set rac_sequence : %@", x);
    }];

    //RACTuple 会返回内置数组的元素
    [tuple.rac_sequence.signal subscribeNext:^(id _Nullable x) {
        NSLog(@"tuple rac_sequence : %@", x);
    }];

    //以下是输出结果,从结果可以看出,结果是乱序的
    //如果再打印当前线程,会发现每种集合都在各自的一条线程上输出,但非主线程
    //array rac_sequence : 1
    //array rac_sequence : 2
    //array rac_sequence : 3
    //str rac_sequence : A
    //set rac_sequence : 3
    //str rac_sequence : B
    //tuple rac_sequence : 1
    //set rac_sequence : 2
    //dict rac_sequence : <RACTuple: 0x610000004cf0> (key1,value1)
    //tuple rac_sequence : 2
    //str rac_sequence : C
    //set rac_sequence : 1
    //dict rac_sequence : <RACTuple: 0x610000004ce0> (key3,value3)
    //tuple rac_sequence : 3
    //dict rac_sequence : <RACTuple: 0x608000004bb0> (key2,value2)
上一篇下一篇

猜你喜欢

热点阅读