objective-c - 如何在 Objective-C 中反转 NSArray?

我需要反转我的 NSArray

举个例子:

[1,2,3,4,5] 必须变为:[5,4,3,2,1]

实现这一目标的最佳方法是什么?

最佳答案

如果您利用 NSArray 上的内置 reverseObjectEnumerator 方法和NSEnumerator:

NSArray* reversedArray = [[startArray reverseObjectEnumerator] allObjects];

allObjects is documented作为返回一个数组,其中包含尚未用 nextObject 遍历的对象,按顺序:

This array contains all the remaining objects of the enumerator in enumerated order.

https://stackoverflow.com/questions/586370/

相关文章:

objective-c - 使用 alloc init 而不是 new

ios - 何时使用 NSInteger 与 int

ios - 如何删除 UITableView 中的空单元格?

objective-c - objectForKey 和 valueForKey 的区别?

ios - 将 NSURL 转换为 NSString

objective-c - Xcode 的隐藏功能

ios - 在 Objective-C 中使用 GCD 的 dispatch_once 创建单例

objective-c - 如何按比例缩放 UIImageView?

objective-c - 我的 Objective-C 单例应该是什么样的?

objective-c - "sending ' 常量 NSString * ' to parame