How does one mirror an NSMutableArray
(first element becomes last one, second becomes second to last, ...)?
Asked
Active
Viewed 129 times
1

Fatso
- 1,278
- 16
- 46
2 Answers
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
-
1
-
1There's a small error btw : it should be `c - i - 1` since `c - 0` is out of bounds. – Fatso Jan 28 '13 at 14:25