Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there an easy way to iterate over an NSArray backwards?

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.

like image 529
Thanks Avatar asked May 09 '09 22:05

Thanks


People also ask

How do you reverse an NSArray?

You can reverse a NSArray by writing your own loop iterating from the end towards the beginning and using a second array to add the items in reverse order. Or you can simply use - (NSEnumerator *)reverseObjectEnumerator from the NSArray class.

How do I create an NSArray in Objective C?

Creating NSArray Objects Using Array Literals In addition to the provided initializers, such as initWithObjects: , you can create an NSArray object using an array literal. In Objective-C, the compiler generates code that makes an underlying call to the init(objects:count:) method.

How do you create an integer array in Objective C?

You can use a plain old C array: NSInteger myIntegers[40]; for (NSInteger i = 0; i < 40; i++) myIntegers[i] = i; // to get one of them NSLog (@"The 4th integer is: %d", myIntegers[3]);


5 Answers

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

like image 68
Sijmen Mulder Avatar answered Sep 25 '22 18:09

Sijmen Mulder


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.

like image 32
5 revs, 2 users 94% Avatar answered Sep 27 '22 18:09

5 revs, 2 users 94%


From here:

 NSEnumerator* myIterator = [myArray reverseObjectEnumerator];
 id anObject;

 while( anObject = [myIterator nextObject])
 {
     /* do something useful with anObject */
 }
like image 44
Naaff Avatar answered Sep 26 '22 18:09

Naaff


[NsArray reverseObjectEnumerator]
like image 32
CiNN Avatar answered Sep 27 '22 18:09

CiNN


for (int i = ((int)[array count] - 1); i > -1; i--) {
    NSLog(@"element: %@",array[i]);
}
like image 45
Vyacheslav Zubenko Avatar answered Sep 24 '22 18:09

Vyacheslav Zubenko