How do I reverse the order of objects in an NSMutableArray? [duplicate]

蹲街弑〆低调 提交于 2019-11-29 02:11:01

问题


This question already has an answer here:

  • How can I reverse a NSArray in Objective-C? 18 answers

I'm trying to achieve something like this

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


回答1:


Simply use;

NSArray* reversed = [[myArray reverseObjectEnumerator] allObjects];

The order is documented to be correct:

This array contains all the remaining objects of the enumerator in enumerated order [emphasis added].



来源:https://stackoverflow.com/questions/2079378/how-do-i-reverse-the-order-of-objects-in-an-nsmutablearray

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!