Keep track of index in fast enumeration

后端 未结 5 1170
日久生厌
日久生厌 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 12:57

    I suppose the most blunt solution to this would be to simply increment an index manually.

    NSUInteger indexInSavedArray = 0;
    for (MyClass *entry in savedArray) {
       indexInSavedArray++;
     }
    

    Alternatively, you could just not use fast enumeration.

        for (NSUInteger indexInSavedArray = 0; indexInSavedArray < savedArray.count; indexInSavedArray++) {
           [savedArray objectAtIndex:indexInSavedArray];
         }
    

提交回复
热议问题