Keep track of index in fast enumeration

后端 未结 5 1159
日久生厌
日久生厌 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条回答
  •  萌比男神i
    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;
    }
    

提交回复
热议问题