遍历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;
                            }
                        }]

 

posted @ 2016-04-25 18:36  imKirin  阅读(1628)  评论(0编辑  收藏  举报