37

I'm trying to achieve something like this

NSMutableArray *myArray = [NSMutableArray arrayWithObjects:@"A",@"B",@"C", nil];
NSLog(@"Array: %@", myArray);
//logs A, B, C.

//reverse code here

 NSLog(@"Array: %@", myArray);
//logs C, B, A

The code isn't exact, just a demo. But you get the idea.

Any way to do this?

Monolo
  • 18,205
  • 17
  • 69
  • 103
Sam Jarman
  • 7,277
  • 15
  • 55
  • 100
  • I found this similar post on stackOverflow: http://stackoverflow.com/questions/586370/how-can-i-reverse-a-nsarray-in-objective-c I think it will fit your requirements as well. – D.C. Jan 16 '10 at 23:35

1 Answers1

164

Simply use;

NSArray* reversed = [[myArray reverseObjectEnumerator] allObjects];

The order is documented to be correct:

This array contains all the remaining objects of the enumerator in enumerated order [emphasis added].

jscs
  • 63,694
  • 13
  • 151
  • 195
EEE
  • 4,536
  • 3
  • 28
  • 34