使用索引快速列舉 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

}];