遍历NSArray, NSDictionary, NSSet的方法总结

1,for循环读取

 1 NSArray:
 2 NSArray *array = /*…*/
 3 for(int i=0; i<array.count; i++)
 4 {
 5      id object = array[i];
 6      // do sth
 7 }
 8
 9 NSDictionary:
10 NSDictionary *dic = /*…*/
11 NSArray *keys = [dic allKeys];
12 for(int i=0; i<keys.count; i++)
13 {
14      id key = keys[i];
15      id value = dic[key];
16      //do sth
17 }
18
19 NSSet:
20 NSSet *set = /*…*/
21 NSArray *objects = [set allObjects];
22 for(int i=0; i<objects.count; i++)
23 {
24      id object = objects[i];
25      //do sth
26 }

2, NSEnumerator类来循环

NSArray:
NSArray *array = /*…*/
NSEnumerator *enumerator = [array objectEnumerator];
id object;
while((object = [enumerator nextObject]) != nil)
{
    //do sth with ‘object‘
}

NSDictionary:
NSDictionary *dic = /*…*/
NSEnumerator *enumerator = [dic keyEnumerator];
id key;
while((key = [enumerator nextObject]) != nil)
{
    id value = dic[key];
    //do sth
}

NSSet:
NSSet *set = /*…*/
NSEnumerator *enumerator = [set objectEnumerator];
id object;
while((object = [enumerator nextObject]) != nil)
{
    //do sth with ‘object‘
}

//逆序
NSArray:
NSArray *array = /*…*/
NSEnumerator *enumerator = [array reverseObjectEnumerator];
id object;
while((object = [enumerator nextObject]) != nil)
{
    //do sth with ‘object‘
}

3, 快速遍历

NSArray:
NSArray *array = /*…*/
for(id object in array){
    //do sth with ‘object‘
}

NSDictionary:
NSDictionary *dic = /*…*/
for(id key in dic){
    id value = dic[key];
    //do sth with ‘value‘
}

NSSet:
NSSet *set = /*…*/
for(id object in set){
    //do sth with ‘object‘
}

//逆序
NSArray:
NSArray *array = /*…*/
for(id object in [array reverseObjectEnumerator]){
    //do sth with ‘object‘
}

4, 块枚举法

NSArray:        -(void) enumerateObjectsUsingBlock:(void(^)(id object, NSUInteger idx, BOOL *stop)) block;
NSDictionary:    -(void) enumerateKeysAndObjectsUsingBlock(void(^)(id key, id object, BOOL *stop)) block;
NSSet:            -(void) enumerateObjectsUsingBlock:(void(^)(id object, BOOL *stop)) block;

逆序或者并发遍历,使用带有‘选项掩码’的块遍历方法:
NSArray:        -(void) enumerateObjectsWithOptions:(NSEnumerationOptions)options
                                         usingBlock:(void(^)(id object, NSUInteger idx, BOOL *stop)) block;
NSDictionary:    -(void) enumerateKeysAndObjectsWithOptions:(NSEnumerationOptions)options
                                                usingBlock:(void(^)(id key, id object, BOOL *stop)) block;
NSSet:            -(void) enumerateObjectsWithOptions:(NSEnumerationOptions)options
                                         usingBlock:(void(^)(id object, BOOL *stop)) block;

其中:NSEnumerationOptions类型是个Enum,其各种取值可用“按位或”(bitwise OR)连接,用以表明遍历方式。
NSEnumerationConcurrent:并发方式执行各轮迭代
NSEnumerationReverse:逆向

NSArray:
NSArray *array = /*…*/
[array enumerateObjectsUsingBlock:
    ^(id object, NSUInteger idx, BOOL *stop){
        //do sth with ‘object‘
        if(shouldstop){
            stop = YES;
        }
    }]

NSDictionary:
NSDictionary *dic = /*…*/
[dic enumerateKeysAndObjectsUsingBlock:
    ^(id key, id object, BOOL *stop){
        //do sth
        if(shouldstop){
            stop = YES;
        }
    }]

NSSet:
NSSet *set = /*…*/
[set enumerateObjectsUsingBlock:
    ^(id object, BOOL* stop){
        //do sth
        if(shouldstop){
            stop = YES;
        }
    }]

//逆序、并发
NSArray:
NSArray *array = /*…*/
[array enumerateObjectsWithOptions:NSEnumerationReverse|NSEnumerationConcurrent
                        usingBlock:^(id object, NSUInteger idx, BOOL* stop){
                            //do sth
                            if(shouldstop){
                                stop = YES;
                            }
                        }]
时间: 2024-11-03 21:21:04

遍历NSArray, NSDictionary, NSSet的方法总结的相关文章

[转]一些NSArray,NSDictionary,NSSet相关的算法知识

iOS编程当中的几个集合类:NSArray,NSDictionary,NSSet以及对应的Mutable版本,应该所有人都用过.只是简单使用的话,相信没人会用错,但要做到高效(时间复杂度)精确(业务准确性),还需要了解其中所隐藏的算法知识. 在项目当中使用集合类几乎是不可避免的,集合类的使用场景其实可以进行抽象的归类.大多数时候我们需要将若干个对象(object)暂时保存起来,以备后续的业务逻辑进行操作,「保存和操作」,或者说「存与取」,对应到计算机世界的术语就是读和写.最初保存的时候我们Ins

oc/object-c/ios哪种遍历NSArray/NSDictionary方式快?测试报告

做app的时候,总免不了要多次遍历数组或者字典.究竟哪种遍历方式比较快呢?我做了如下测试:首先定义测试用宏: ? 1 2 3 4 5 6 7 8 9 #define MULogTimeintervalBegin(INFO) NSTimeInterval start = [NSDate timeIntervalSinceReferenceDate];\ NSTimeInterval duration = 0;\ NSLog(@"MULogTimeintervalBegin:%@", IN

Fouandation(NSString ,NSArray,NSDictionary,NSSet) 中常见的理解错误区

Fouandation 中常见的理解错误区 1.NSString //快速创建(实例和类方法) 存放的地址是 常量区 NSString * string1 = [NSString alloc]initWithString:@“123”]; NSString * string3 = [NSString stringWithString:@“123”]; //格式化方法创建   存放地址是堆区 NSString * string2 = [NSString alloc]initWithFormat:@

NSArray,NSDictionary,NSSet总结

-学习笔记,以备以后查询 ------干货分割线------------------- -NSArray 1.初始化.取值等基础操作 -NSArray 是不可变数组,一旦创建完成就不能够对数组进行,添加,删除等操作 NSArray * array = [[NSArray alloc] init]; NSArray * array1 = [NSArray                        arrayWithObjects:@"one",@"two",@&q

Read and Write NSArray, NSDictionary and NSSet to a File

查询地址:http://iosdevelopertips.com/data-file-management/read-and-write-nsarray-nsdictionary-and-nsset-to-a-file.html With just a few lines of code, you can read/write collections to/from files. The code below shows examples for writing and reading both

重写NSArray与NSDictionary的descriptionWithLocale方法

重写NSArray的descriptionWithLocale方法: NSArray+log.h #import <Foundation/Foundation.h> @interface NSArray (log) @end NSArray+log.m #import "NSArray+log.h" @implementation NSArray (log) - (NSString *)descriptionWithLocale:(id)locale { NSMutable

iOS - 数组与字典(NSArray &amp; NSDictionary)

1. 数组的常用处理方式 //--------------------不可变数组 //1.数组的创建 NSString *s1 = @"zhangsan"; NSString *s2 = @"lisi"; NSString *s3 = @"wangwu"; //(1) NSArray *array1 = [[NSArray alloc] initWithObjects:s1,s2,s3, nil]; NSLog(@"%@",a

Xcode4.4(LLVM4.0编译器)中NSArray, NSDictionary, NSNumber优化写法

Xcode4.4(LLVM4.0编译器)中NSArray, NSDictionary, NSNumber优化写法 从xcode4.4开始,LLVM4.0编译器为Objective-C添加一些新的特性.创建数组NSArray,字典NSDictionary, 数值对象NSNumber时,可以像NSString的初始化一样简单方便.妈妈再也不担心程序写得手发酸了. A.   NSArray 首先是非常常用的NSArray,NSMutableArray.NSArray是一个初始化后就固定的静态数组.如果

NSData NSDate NSString NSArray NSDictionary 相互转化

//    NSData  NSDate NSString NSArray NSDictionary json NSString *string = @"hello word"; NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding]; NSArray *array = [NSArray arrayWithObjects:@"1",@"2",@"3",nil