开发者

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

开发者 https://www.devze.com 2022-12-16 10:18 出处:网络
This question already has answers here: 开发者_JS百科 How can I reverse a NSArray in Objective-C? (18 answers)
This question already has answers here: 开发者_JS百科 How can I reverse a NSArray in Objective-C? (18 answers) Closed 9 years ago.

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?


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].

0

精彩评论

暂无评论...
验证码 换一张
取 消