美文网首页
iOS中的几种遍历方式

iOS中的几种遍历方式

作者: brilliance_Liu | 来源:发表于2017-03-01 16:34 被阅读48次
    void FastEnumration(){
        
        NSArray *array = @[@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"10"];
        //一、For Statement
        for (int i = 0 ; i<array.count; i++) {
            NSLog(@"for statement %@---%@",array[i],[NSThread currentThread]);
        }
        
        
        //二、快速排序
        for (NSString *str in array) {
            NSLog(@"for fast enumration %@---%@",str,[NSThread currentThread]);
        }
        
    
        //三、NSEnumerator
        NSEnumerator *enumer=[array objectEnumerator];
        id obj;
        while (obj=[enumer nextObject]) {
            NSLog(@"objectEnumerator %@----%@",obj,[NSThread currentThread]);
        }
    
    
        //四、enumerateObjectsUsingBlock
        //顺序遍历
        [array enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            NSLog(@"顺序遍历 %@----%@",array[idx],[NSThread currentThread]);
        }];
        
    
        //倒序遍历
        [array enumerateObjectsWithOptions:NSEnumerationReverse usingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            NSLog(@"倒序遍历 %@----%@",array[idx],[NSThread currentThread]);
        }];
        
    
        //五、快速迭代
        //将block中的任务,逐个放到queue中,然后进行dispatch_sync执行
        //多线程同步循环
    
        dispatch_queue_t queue =dispatch_queue_create("apply并行队列", DISPATCH_QUEUE_CONCURRENT);
        dispatch_apply([array count], queue, ^(size_t index) {
            NSLog(@"%@----%@",array[index],[NSThread currentThread]);
        });
    }
    
    
    
    For Statement.png fast enumration.png NSEnumerator.png 顺序遍历.png 倒序遍历.png 快速迭代.png

    总结,从数据来看,性能差异较小,所以大家视使用场景,自行决定使用哪一个。(数据量较大时,更易看出,感兴趣的请自行运行代码测试)再次申明:内容是本人学习之互联网,并做笔记之用。


    相关文章

      网友评论

          本文标题:iOS中的几种遍历方式

          本文链接:https://www.haomeiwen.com/subject/bqqcgttx.html