使用索引快速枚举 NSArray

此示例显示如何使用快速枚举以遍历 NSArray。通过这种方式,你还可以在遍历时跟踪当前对象的索引。

假设你有一个数组,

NSArray *weekDays = @[@"Monday", @"Tuesday", @"Wednesday", @"Thursday", @"Friday", @"Saturday", @"Sunday"];

现在你可以像下面那样遍历数组,

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

    //... Do your usual stuff here

    obj  // This is the current object
    idx  // This is the index of the current object
    stop // Set this to true if you want to stop

}];