我需要在 iOS
中使用 NSArray
执行类似于 python 的 enumerate()
函数的操作(我还必须构建 NSIndexPath 对象)作为检查对象)。
我没有看到一个内置方法可以做这样的事情(即没有相当于 NSDictionary 的 enumerateKeysAndObjectsUsingBlock: 方法的 NSArray )。这让我想到了两种我能想到的通用方法。
for (NSUInteger index = 0; index < mySequence.count; index++) {
MyElementType *element = mySequence[index];
//
// code that works with both index and element
//
}
或
NSUInteger index = 0;
for (MyElementType *element in mySequence) {
//
// code that works with both index and element
//
index++;
}
有充分的理由选择其中一个吗?或者是否有第三种方法比这两种方法更好?
最佳答案
NSArray 中存在以下 API:
- (void)enumerateObjectsUsingBlock:(void (^)(id obj, NSUInteger idx, BOOL *stop))
关于ios - 通过索引和元素枚举 NSArray 的惯用方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27139156/