IOS,遍历的几种方法总结

1,for循环,最基础常用的

NSArray  * array = @[@"1",@"2",@"3",@"4",@"5",@"6"];
    for (int i = 0; i < array.count; i++) {
        NSLog(@"%d",i);
    }

 2, for in 循环

    for (NSString * str in array) {
        NSLog(@"%@",str);
    }

3, 枚举器

    [array enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL *     _Nonnull stop) {
        
    }];

    //NSEnumerationReverse 倒叙 NSEnumerationConcurrent 大概顺序,多核遍历
    [array enumerateObjectsWithOptions:NSEnumerationReverse usingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        
    }];

4.dispatch_apply 函数

GCD dispatch_apply函数是一个同步调用,block任务执行n次后才返回。该函数比较适合处理耗时较长、迭代次数较多的情况。

    dispatch_apply(array.count, dispatch_get_global_queue(0, 0), ^(size_t index) {
        NSLog(@"%@---%zu",[NSThread currentThread],index);
    });

5。ReactiveCocoa 遍历

//数组遍历
    [self.traverseArray.rac_sequence.signal subscribeNext:^(id x) {
 
 
        NSLog(@"%@",x);
    }];

//字典遍历 相当于元组数据
    [self.traverseDictionary.rac_sequence.signal subscribeNext:^(id x) {
        // 解包元组,会把元组的值,按顺序给参数里面的变量赋值
        RACTupleUnpack(NSString *key,NSString*value) = x;
        
        NSLog(@"key=%@ value=%@",key,value);
        
    }];

 

posted @ 2020-09-18 22:28  时光清浅、  阅读(720)  评论(0)    收藏  举报