开发者

"Reverse" NSMutableArray's elements order [duplicate]

开发者 https://www.devze.com 2023-02-10 11:38 出处:网络
This question already has answers here: 开发者_StackOverflow中文版 Closed 11 years ago. Possible Duplicate:
This question already has answers here: 开发者_StackOverflow中文版 Closed 11 years ago.

Possible Duplicate:

How can I reverse a NSArray in Objective-C?

Is there a way to reverse the elements in a NSMutableArray?


How can I reverse a NSArray in Objective-C?


I don't think that there is an easy way to do it. Best way that I came up with is the exchangeObjectAtIndex:withObjectAtIndex: function.

pseudo code:

NSMutableArray* array = [NSMutableArray arrayWithObjects:@"1", @"2", @"3",nil];
for(i = 0, len = (int)[array count]/2; i < len; i++)
{
  [array exchangeObjectAtIndex:i withObjectAtIndex:([array count] - i - 1)];
}

Don't have the compiler open right at this moment but hopefully this does the trick.

0

精彩评论

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