Keep track of index in fast enumeration

后端 未结 5 1161
日久生厌
日久生厌 2020-12-24 12:29

I want to get the index of the current object when using fast enumeration, i.e.

for (MyClass *entry in savedArray) {
// What is the index of |entry| in |sav         


        
5条回答
  •  甜味超标
    2020-12-24 13:12

    This question has already been answered, but I thought I would add that counting iterations is actually the technique mentioned in the iOS Developer Library documentation:

    NSArray *array = <#Get an array#>;
    NSUInteger index = 0;
    
    for (id element in array) {
        NSLog(@"Element at index %u is: %@", index, element);
        index++;
    }
    

    I was sure there would be a fancy trick, but I guess not. :)

提交回复
热议问题