Block 代替for循环

  NSDictionary *aDictionary = [[NSDictionary alloc]initWithObjectsAndKeys:@"tom",@"1",@"alex",@"2",@"josen",@"3", nil];
        [aDictionary enumerateKeysAndObjectsUsingBlock:^(id key, id value, BOOL *stop) {
            NSLog(@"value for key %@ is %@", key, value);
            if ([@"2" isEqualToString:key]) {
                *stop = YES; }
        }];
        
        NSArray *aArray = [[NSArray alloc]initWithObjects:@"1",@"2",@"3",@"4",@"5", nil];
        [aArray enumerateObjectsUsingBlock:^(id obj, NSUInteger index, BOOL *stop){
             NSLog(@"----%@ ---%ld",[aArray objectAtIndex:index],index);
            if (index == 2) {
                *stop = YES;

            }
        }];

 

posted @ 2014-06-11 15:09  菜鸟程序猿  阅读(375)  评论(0编辑  收藏  举报