我需要反转NSArray。

举个例子:

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

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


当前回答

反向枚举数组的最有效方法:

使用enumerateObjectsWithOptions:NSEnumerationReverse usingBlock。使用@JohannesFahrenkrug上面的基准测试,这比[[array reverseObjectEnumerator] allObjects]快8倍;:

NSDate *methodStart = [NSDate date];

[anArray enumerateObjectsWithOptions:NSEnumerationReverse usingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
    //
}];

NSDate *methodFinish = [NSDate date];
NSTimeInterval executionTime = [methodFinish timeIntervalSinceDate:methodStart];
NSLog(@"executionTime = %f", executionTime);

其他回答

试试这个:

for (int i = 0; i < [arr count]; i++)
{
    NSString *str1 = [arr objectAtIndex:[arr count]-1];
    [arr insertObject:str1 atIndex:i];
    [arr removeObjectAtIndex:[arr count]-1];
}

Georg Schölly的分类非常好。然而,对于NSMutableArray,当数组为空时,使用NSUIntegers作为索引会导致崩溃。正确的代码是:

@implementation NSMutableArray (Reverse)

- (void)reverse {
    NSInteger i = 0;
    NSInteger j = [self count] - 1;
    while (i < j) {
        [self exchangeObjectAtIndex:i
                  withObjectAtIndex:j];

        i++;
        j--;
    }
}

@end

一些基准

1. reverseObjectEnumerator allObjects

这是最快的方法:

NSArray *anArray = @[@"aa", @"ab", @"ac", @"ad", @"ae", @"af", @"ag",
        @"ah", @"ai", @"aj", @"ak", @"al", @"am", @"an", @"ao", @"ap", @"aq", @"ar", @"as", @"at",
        @"au", @"av", @"aw", @"ax", @"ay", @"az", @"ba", @"bb", @"bc", @"bd", @"bf", @"bg", @"bh",
        @"bi", @"bj", @"bk", @"bl", @"bm", @"bn", @"bo", @"bp", @"bq", @"br", @"bs", @"bt", @"bu",
        @"bv", @"bw", @"bx", @"by", @"bz", @"ca", @"cb", @"cc", @"cd", @"ce", @"cf", @"cg", @"ch",
        @"ci", @"cj", @"ck", @"cl", @"cm", @"cn", @"co", @"cp", @"cq", @"cr", @"cs", @"ct", @"cu",
        @"cv", @"cw", @"cx", @"cy", @"cz"];

NSDate *methodStart = [NSDate date];

NSArray *reversed = [[anArray reverseObjectEnumerator] allObjects];

NSDate *methodFinish = [NSDate date];
NSTimeInterval executionTime = [methodFinish timeIntervalSinceDate:methodStart];
NSLog(@"executionTime = %f", executionTime);

结果:executionTime = 0.000026

2. 迭代一个reverseObjectEnumerator对象

这要慢1.5到2.5倍:

NSDate *methodStart = [NSDate date];
NSMutableArray *array = [NSMutableArray arrayWithCapacity:[anArray count]];
NSEnumerator *enumerator = [anArray reverseObjectEnumerator];
for (id element in enumerator) {
    [array addObject:element];
}
NSDate *methodFinish = [NSDate date];
NSTimeInterval executionTime = [methodFinish timeIntervalSinceDate:methodStart];
NSLog(@"executionTime = %f", executionTime);

结果:executionTime = 0.000071

3.sortedArrayUsingComparator

这要慢30到40倍(这里没有意外):

NSDate *methodStart = [NSDate date];
NSArray *reversed = [anArray sortedArrayUsingComparator: ^(id obj1, id obj2) {
    return [anArray indexOfObject:obj1] < [anArray indexOfObject:obj2] ? NSOrderedDescending : NSOrderedAscending;
}];

NSDate *methodFinish = [NSDate date];
NSTimeInterval executionTime = [methodFinish timeIntervalSinceDate:methodStart];
NSLog(@"executionTime = %f", executionTime);

结果:executionTime = 0.001100

因此,当谈到速度和易用性时,[[anArray reverseObjectEnumerator] allObjects显然是赢家。

在看了上面其他人的答案并在这里找到马特·加拉格尔的讨论后

我建议:

NSMutableArray * reverseArray = [NSMutableArray arrayWithCapacity:[myArray count]]; 

for (id element in [myArray reverseObjectEnumerator]) {
    [reverseArray addObject:element];
}

正如Matt所观察到的:

在上面的例子中,你可能想知道-[NSArray reverseObjectEnumerator]是否会在循环的每次迭代中运行-可能会降低代码的速度。<…>

此后不久,他这样回答:

<……当for循环开始时,"collection"表达式只计算一次。这是最好的情况,因为您可以安全地将昂贵的函数放在“collection”表达式中,而不会影响循环的每次迭代性能。

有一个更简单的解决方案,如果你利用NSArray内置的reverseObjectEnumerator方法,以及NSEnumerator的allObjects方法:

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

allObjects被记录为返回一个数组,其中的对象还没有被nextObject遍历,顺序如下:

该数组按枚举顺序包含枚举器的所有剩余对象。