美文网首页
iOS数组遍历

iOS数组遍历

作者: Arthur澪 | 来源:发表于2019-07-11 17:03 被阅读0次

对于一个数组

    NSArray *array = @[@"111",@"222",@"333",@"444",@"555",@"666",@"777",@"888",@"999",];

    NSInteger count =array.count;

1.for循环

for (NSInteger i=0; i<count; i++) {
   NSLog(@"%@----%@",array[i],[NSThread currentThread]);
}

2.for in快速枚举

for (NSString *string in array) {
   NSLog(@"%@----%@",string,[NSThread currentThread]);
}

集合中对象数很多的情况下,for in 的遍历速度非常之快。但小规模的遍历 还没for循环快。

3. 枚举器NSEnumerator

    // 向数组请求枚举器
    NSEnumerator *enumer = [array objectEnumerator];  // 正序
    NSEnumerator *enumer2 = [array reverseObjectEnumerator];  // 倒序
   
    id obj;
    while ( obj = [enumer nextObject]) {
        // nextObject为nil,结束循环
        // 不可对array的元素进行增 删
        NSLog(@"%@----%@",obj,[NSThread currentThread]);
    }

4. enumerateObjectsUsingBlock方法

    // 顺序遍历
    [array enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        NSLog(@"%@----%@",array[idx],[NSThread currentThread]);
        
        if (idx == 5) {
            *stop = YES;   // 停止遍历
        }
    }];


    // 倒序遍历
    [array enumerateObjectsWithOptions:NSEnumerationReverse usingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        NSLog(@"%@----%@",array[idx],[NSThread currentThread]);

        if (idx == 5) {
            *stop = YES;   // 停止遍历
        }
    }];

Block内代码可以并发执行。

字典情况下

    NSDictionary * dic = [NSDictionary dictionary];
    
    [dic enumerateKeysAndObjectsUsingBlock:^(id key, id value, BOOL *stop) {
        NSLog(@"value for key %@ is %@ ", key, value);
        if ([@"key2" isEqualToString:key]) {
            *stop = YES;
        }
    }];

遍历字典类型时,推荐使用。字典遍历可以同时取allkeys和allvalues中的元素。

5.多线程dispatch_apply

// 并行队列
dispatch_queue_t queue = dispatch_queue_create("zzz", DISPATCH_QUEUE_CONCURRENT);
    
dispatch_apply(count, queue, ^(size_t index) {
    NSLog(@"%@----%@",array[index],[NSThread currentThread]);
});
 

适用于 处理每一次循环都有耗时任务的数组遍历。

6.NSSet

 NSArray *arr1 = ....;
 NSArray *arr2 =....;

 NSSet *aaaSet = [NSSet setWithArray:arr2];

 for (NSUInteger i = 0; i < arr1.count; ++i) {
    UIView *targetView = arr1[i];
    if ([aaaSet containsObject:targetView]) {
        //....
    }
 }

数组排序

    NSArray * allKeys = @[@"2",@"1",@"4",@"8"];
// 对数组进行排序。
    NSArray *sortedKeys = [allKeys sortedArrayUsingComparator:^NSComparisonResult(id  _Nonnull obj1, id  _Nonnull obj2) {
        
        NSNumber *number1 = [NSNumber numberWithInt:[obj1 intValue]];
        NSNumber *number2 = [NSNumber numberWithInt:[obj2 intValue]];
        NSComparisonResult result = [number1 compare:number2];

        return  result == NSOrderedDescending;  // 升序
    }];

相关文章

  • iOS 数组 NSArray 遍历 懒加载总结

    iOS开发之懒加载 iOS中数组遍历的方法及比较

  • iOS数组遍历

    对于一个数组 1.for循环 2.for in快速枚举 集合中对象数很多的情况下,for in 的遍历速度非常之快...

  • iOS 性能优化

    iOS App 启动性能优化iOS离屏渲染优化(附DEMO) iOS Objective-C 数组遍历的性能及原理...

  • iOS 常用的数据处理

    1. iOS遍历数组的同时删除元素 NSMutableArray*array = [NSMutableArraya...

  • OC中的各种遍历

    OC中的各种遍历 iOS开发中我们经常遇到各种的数据处理,所以就会经常遇到数组遍历 和 字典遍历。但是不同的遍历方...

  • angular2foreach遍历的几种用法

    遍历简单的数组 遍历数组对象 遍历嵌套数组

  • VS常用四种遍历数组的方法

    目录 for 遍历数组 for in 遍历数组 for of 遍历数组 forEach遍历数组 优缺点总结原文:h...

  • foreach/forin

    1.for/in遍历属性,数组是遍历下标 for/each遍历属性值,数组遍历数组的值

  • iOS 模型数组去重

    iOS 模型数组去重 Tip:不要对一个可变数组遍历的同时增删改查!!!! NSMutableArray *tem...

  • ios 数组遍历问题

    起因 经常处理数据的时候 会遇到这样的问题。看到其他语言如swift 或者python 都有自己的map filt...

网友评论

      本文标题:iOS数组遍历

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