enumerateObjectsUsingBlock、enumerateObjectsWithOptions、enumerateObjectsAtIndexes、makeObjectsPerfor使用

OC为 NSArray提供了方便的遍历block,下面进行详细的说明

第一、enumerateObjectsUsingBlock

NSArray *array=@[@"aa",@"bb",@"cc",@"dd",@"ee",@"ff",@"gg"];

      [array enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {

          NSLog(@"%@",obj);

      }];

idx代表索引值,obj代表遍历内容

第二、enumerateObjectsWithOptions

NSEnumerationReverse表示逆序遍历

      [array enumerateObjectsWithOptions:NSEnumerationReverse usingBlock:^(id obj, NSUInteger idx, BOOL *stop) {

          NSLog(@"%@",obj);

      }];

第三、enumerateObjectsAtIndexes

该函数不但可以指定遍历顺序,还可以指定遍历的空间

      [array enumerateObjectsAtIndexes:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(2, 3)] options:NSEnumerationConcurrent usingBlock:^(id obj, NSUInteger idx, BOOL *stop) {

          NSLog(@"%@",obj);

      }];

第五、- (void)makeObjectsPerformSelector:(SEL)aSelector;

让数组中的每个元素 都调用 aMethod

第六、让数组的每个元素执行aSelector,传入的参数是argument

  • (void)makeObjectsPerformSelector:(SEL)aSelector withObject:(id)argument;

(本处做收藏使用。)

版权声明:本文为CSDN博主「richard_飞飞」的原创文章。

你可能感兴趣的:(enumerateObjectsUsingBlock、enumerateObjectsWithOptions、enumerateObjectsAtIndexes、makeObjectsPerfor使用)