您的位置:首页 > 移动开发 > Objective-C

enumerateObjectsUsingBlock、enumerateObjectsWithOptions、enumerateObjectsAtIndexes、makeObjectsPerfor使用

2015-07-02 09:21 936 查看
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;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: