Keep track of index in fast enumeration

后端 未结 5 1133
日久生厌
日久生厌 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:55

    A simple observation: If you initialize the index to -1 and then put the ++index as the first line in the for loop, doesn't that cover all bases (provided someone doesn't slip code in front of the increment)?

    0 讨论(0)
  • 2020-12-24 12:56

    If you want to access the index or return outside block here is a piece of code that can be useful. (considering the array is an array of NSString).

    - (NSInteger) findElemenent:(NSString *)key inArray:(NSArray *)array
    {
        __block NSInteger index = -1;
        [array enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
            if ([obj isEqualToString:key]) {
                *stop = YES;
                index = idx;
            }
        }];
        return index;
    }
    
    0 讨论(0)
  • 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];
         }
    
    0 讨论(0)
  • 2020-12-24 13:11

    Look at the API for NSArray and you will see the method

    - (void)enumerateObjectsUsingBlock:(void (^)(id obj, NSUInteger idx, BOOL *stop))block
    

    So give that one a try

    [savedArray 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
    
    }];
    
    0 讨论(0)
  • 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. :)

    0 讨论(0)
提交回复
热议问题