首页 > 代码库 > Block 代替for循环

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;

            }
        }];