The following method will return the array with items in reverse order. The input given to this method is the NSMutableArray, the contents of which you want to reverse and the output will the NSMutableArray with the values reversed.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
- (NSMutableArray *)reverseArray:(NSMutableArray *)arraytoReverse { if ([arraytoReverse count] == 0) return arraytoReverse; // Return from the method if the array is empty. NSUInteger start = 0; // Setting the initial counter. NSUInteger end = [arraytoReverse count] - 1; // Setting the final counter. while (start < end) { // Iterating through the contents of the array. [arraytoReverse exchangeObjectAtIndex: start withObjectAtIndex: end]; start ++; end--; } return arraytoReverse; // Returning the array with values reversed. } |
Incoming search terms:
- reverse nsmutablearray iphone
Filed under: Cocoa • Cocos2d • iPad • iPhone • Objective C • Tips and Tricks
Like this post? Subscribe to my RSS feed and get loads more!




Leave a Reply