1

How does one mirror an NSMutableArray (first element becomes last one, second becomes second to last, ...)?

Fatso
  • 1,278
  • 16
  • 46

2 Answers2

4
NSArray* reversedArray = [[orgArray reverseObjectEnumerator] allObjects];
mrueg
  • 8,185
  • 4
  • 44
  • 66
Parag Bafna
  • 22,812
  • 8
  • 71
  • 144
  • This will not change your original array, however, i.e the mirroring won't be in-place. – mrueg Jan 28 '13 at 12:17
3

Use -[NSMutableArray exchangeObjectAtIndex:withObjectAtIndex:].

So, if your array is called a:

NSUInteger c = [a count];
for(NSUInteger i = 0; i < (c / 2); ++i) {
    [a exchangeObjectAtIndex:i withObjectAtIndex:(c - i) - 1];
}
Peter Hosey
  • 95,783
  • 15
  • 211
  • 370
mrueg
  • 8,185
  • 4
  • 44
  • 66