I've got an NSArray and have to iterate over it in a special case backwards, so that I first look at the last element. It's for performance reasons: If the last one just makes no sense, all previous ones can be ignored. So I'd like to break the loop. But that won't work if I iterate forward from 0 to n. I need to go from n to 0. Maybe there is a method or function I don't know about, so I wouldn't have to re-invent the wheel here.
            Asked
            
        
        
            Active
            
        
            Viewed 2.7k times
        
    72
            
            
         
    
    
        Mike Abdullah
        
- 14,933
- 2
- 50
- 75
 
    
    
        Thanks
        
- 40,109
- 71
- 208
- 322
- 
                    4"How can I reverse a NSArray in Objective-C?" is a completely different question. – Nestor May 30 '14 at 11:44
- 
                    Swift: http://stackoverflow.com/questions/24508592/how-to-iterate-for-loop-in-reverse-order-in-swift – Suragch Aug 11 '16 at 17:53
5 Answers
199
            To add on the other answers, you can use -[NSArray reverseObjectEnumerator] in combination with the fast enumeration feature in Objective-C 2.0 (available in Leopard, iPhone):
for (id someObject in [myArray reverseObjectEnumerator])
{
    // print some info
    NSLog([someObject description]);
}
Source with some more info: http://cocoawithlove.com/2008/05/fast-enumeration-clarifications.html
 
    
    
        Sijmen Mulder
        
- 5,767
- 3
- 22
- 33
- 
                    Is this considered fast enumeration, and is this faster than a regular c for loop that starts from `[myArray count]-1`? How do you iterate over an `NSString` backwards? – ma11hew28 Apr 17 '11 at 03:28
- 
                    I don’t have any links here right now but there is a video somewhere (could’ve been a WWDC) where an Apple engineer explains how fast enumeration works and why it is fast. It outperforms both index-based NSArray enumeration and classic Foundation enumerators and, depending on the scenario, by a lot. – Sijmen Mulder Apr 20 '11 at 00:37
- 
                    
- 
                    4@AshwinG Your question doesn't make sense, as `NSDictionary`s don't have an 'order'. You can enumerate them in any real order, unless you first order the keys/values. – mattsven Feb 24 '16 at 14:03
17
            
            
        Since this is for performace, you have a number of options and would be well advised to try them all to see which works best.
- [array enumerateObjectsWithOptions:NSEnumerationReverse usingBlock:…]
- -[NSArray reverseObjectEnumerator]
- Create a reverse copy of the array and then iterate through that normally
- Use a standard C for loop and start and work backwards through the array.
More extreme methods (if performance is super-critical)
- Read up on how Cocoa implements fast object enumeration and create your own equivalent in reverse.
- Use a C or C++ array.
There may be others. In which case, anyone feel free to add it.
 
    
    
        Community
        
- 1
- 1
 
    
    
        Mike Abdullah
        
- 14,933
- 2
- 50
- 75
- 
                    1I would say that the enumerateObjectsWithOptions:NSEnumerationReverse is the best answer, especially if you need to know what index you are on. Thanks!!! – Vincil Bishop Jul 17 '14 at 01:35
2
            
            
        for (int i = ((int)[array count] - 1); i > -1; i--) {
    NSLog(@"element: %@",array[i]);
}
 
    
    
        Vyacheslav Zubenko
        
- 81
- 4
- 
                    This does not detect changes to a mutable array instance during enumeration (elements, count). Otherwise probably a little faster than creating an NSEnumerator object. – osxdirk May 04 '20 at 09:12
 
    