views:

511

answers:

2

Hey,

Im trying to achieve something like this

NSMutableArray *myArray = [[NSArray alloc] initWithObjects: @"A",@"B",@"C",nil, retain];
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?

Cheers, Sam

+2  A: 

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.

darren
+4  A: 

Simply use;

NSArray* reversed = [[myArray reverseObjectEnumerator] allObjects];
EEE
will this work with mutable array?
Sam Jarman
Yes; an NSMutableArray is, by declaration, a subclass of NSArray.
bbum
Not guaranteed to work. It could break anytime.
Georg