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条)

报告相同问题?

悬赏问题

  • ¥15 关于#java#的问题:找一份能快速看完mooc视频的代码
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!