Rectangle 27 0

iphone How to display an array in reverse order in objective C?


NSArray* reversedArray = [[startArray reverseObjectEnumerator] allObjects];

Its also an exact copy and paste of this answer by somebody else from almost 4 years ago here: stackoverflow.com/a/586529/1026573

That is a pretty good solution, but I notice that Dave DeLong suggested the same thing 2.5 years ago. Did you miss his answer?

There is a much easier solution, if you take advantage of the built-in reverseObjectEnumerator method on NSArray, and the allObjects method of NSEnumerator:

Note
Rectangle 27 0

iphone How to display an array in reverse order in objective C?


-[NSArray reverseObjectEnumerator]
NSArray * a = [NSArray arrayWithObjects:@"a", @"b", @"c", nil];
NSArray * reverse = [[a reverseObjectEnumerator] allObjects];
NSLog(@"%@", reverse);  //logs "c, b, a"
Note
Rectangle 27 0

iphone How to display an array in reverse order in objective C?


cell.title = [anarray objectAtIndex:(anarray.count - indexPath.row - 1)];

Sign up for our newsletter and get our top new questions delivered to your inbox (see an example).

Fixed formatting and changed an erroneous set of [...] to (...)

Well i dont know what your code is but you can do something like this

Note
Rectangle 27 0

iphone How to display an array in reverse order in objective C?


cell.title = [anarray objectAtIndex:(anarray.count - indexPath.row - 1)];

Fixed formatting and changed an erroneous set of [...] to (...)

Well i dont know what your code is but you can do something like this

Note
Rectangle 27 0

iphone How to display an array in reverse order in objective C?


NSArray* reversedArray = [[startArray reverseObjectEnumerator] allObjects];

Its also an exact copy and paste of this answer by somebody else from almost 4 years ago here: stackoverflow.com/a/586529/1026573

That is a pretty good solution, but I notice that Dave DeLong suggested the same thing 2.5 years ago. Did you miss his answer?

There is a much easier solution, if you take advantage of the built-in reverseObjectEnumerator method on NSArray, and the allObjects method of NSEnumerator:

Note
Rectangle 27 0

iphone How to display an array in reverse order in objective C?


-[NSArray reverseObjectEnumerator]
NSArray * a = [NSArray arrayWithObjects:@"a", @"b", @"c", nil];
NSArray * reverse = [[a reverseObjectEnumerator] allObjects];
NSLog(@"%@", reverse);  //logs "c, b, a"
Note