ios - 通过索引和元素枚举 NSArray 的惯用方法

标签 ios objective-c nsarray enumeration

我需要在 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/

相关文章:

search - 在NSArray中搜索字符串

iphone - 在 iPhone 应用程序中隐藏已开发的 iPad 类

ios - FMDB 排队 - 如何正确实现此模式?

ios - Objective-C > TableView 和 ViewController 之间的 Segue

iphone - 从 subview UIViewController Xcode 调用函数

ios - UISplitViewController 垂直拆分而不是水平拆分

iphone - 将 NSMutableArray 转换为 NSDictionary 以便使用 objectForKey?

ios - 如何使用 NSRange 创建 NSArray 的子数组?

iOS5 NSURLConnection 方法已弃用

ios - 以编程方式自动布局功能