开发者

"Reverse" NSMutableArray's elements order [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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜