bug^君 2009-02-25 15:12 采纳率: 25%
浏览 211
已采纳

如何在 Objective-C 中反转 NSArray?

I need to reverse my NSArray.

As an example:

[1,2,3,4,5] must become: [5,4,3,2,1]

What is the best way to achieve this?

转载于:https://stackoverflow.com/questions/586370/how-can-i-reverse-a-nsarray-in-objective-c

  • 写回答

18条回答 默认 最新

  • 谁还没个明天 2009-02-25 15:30
    关注

    For obtaining a reversed copy of an array, look at danielpunkass' solution using reverseObjectEnumerator.

    For reversing a mutable array, you can add the following category to your code:

    @implementation NSMutableArray (Reverse)
    
    - (void)reverse {
        if ([self count] <= 1)
            return;
        NSUInteger i = 0;
        NSUInteger j = [self count] - 1;
        while (i < j) {
            [self exchangeObjectAtIndex:i
                      withObjectAtIndex:j];
    
            i++;
            j--;
        }
    }
    
    @end
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(17条)

报告相同问题?

悬赏问题

  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改