Keep track of index in fast enumeration

后端 未结 5 1164
日久生厌
日久生厌 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: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
    
    }];
    

提交回复
热议问题