【发布时间】:2013-03-23 11:59:39
【问题描述】:
在给定索引之前迭代 NSArray 的索引的最简洁方法是什么?例如:
NSArray *myArray = @[ @"animal" , @"vegetable" , @"mineral" , @"piano" ];
[myArray enumerateObjectsAtIndexes:@"all before index 2" options:nil
usingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
// this block will be peformed on @"animal" and @"vegetable"
}];
此外,如果给定索引为 0,则根本不应该循环。
最简洁、最优雅的方法是什么?到目前为止,我只拼凑了使用烦人的 NSRange 和索引集的笨拙的多行答案。有没有更好的方法可以忽略?
【问题讨论】:
-
任意数量的实现方式。简洁并不意味着清晰。使用实际工作做得好的东西。不要与框架抗争。迭代 NSArray 不需要使用该方法。该方法提供了块使用,这对于块擅长的事情非常有用。
标签: objective-c nsarray iteration nsrange nsindexset