block语法排序 遍历

block语法排序:

 NSMutableArray* array = [NSMutableArray arrayWithObjects:@"6",@"34",@"56",@"78",nil];
        NSComparator sortBlock =^(id obj1,id obj2)
        {
            int object1 = [obj1 intValue];
            int object2 = [obj2 intValue];
            if(object1<object2)
            {
                return NSOrderedDescending;
            }
            else if (object1==object2)
            {
                return NSOrderedSame;
            }
            else
            {
                return NSOrderedAscending;
            }
        };
        NSArray* sortArray = [array sortedArrayUsingComparator:sortBlock];
        NSLog(@"%@",sortArray);
    }
    return 0;
}

block语法遍历:

 NSArray* array = @[@"one",@"two",@"three"];
        //下标遍历
        for(int i=0;i<[array count];i++)
        {
            id obj = [array objectAtIndex:i];
            NSLog(@"%@",obj);
        }
        //for in语法
        for(id obj in array)
        {
            NSLog(@"%@",obj);
        }
        //枚举器
        NSString* str = nil;
        NSEnumerator* enumerator  = [array objectEnumerator];
        while (str = [enumerator nextObject])
        {
            NSLog(@"%@",str);
        }
        //block语法
        [array enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
            NSLog(@"idx=%ld obj=%@",idx,obj);
        }];

 

posted @ 2016-01-08 09:33  33sve  阅读(215)  评论(0编辑  收藏  举报