我需要扭转我的态度NSArray
.
举个例子:
[1,2,3,4,5]
必须成为: [5,4,3,2,1]
实现这一目标的最佳方法是什么?
如果您利用内置reverseObjectEnumerator
方法NSArray
和以下allObjects
方法,有一个更容易的解决方案NSEnumerator
:
NSArray* reversedArray = [[startArray reverseObjectEnumerator] allObjects];
allObjects
记录为返回一个数组,其中包含尚未遍历的对象nextObject
,按顺序:
此数组以枚举顺序包含枚举器的所有剩余对象.
为了得到阵列的反转副本,看danielpunkass'溶液使用reverseObjectEnumerator
.
要反转可变数组,可以在代码中添加以下类别:
@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
一些基准
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.5x到2.5x之间:
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
速度在30x到40x之间(这里没有惊喜):
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]
在速度和轻松方面,明显的赢家也是如此.
DasBoot有正确的方法,但他的代码中有一些错误.这是一个完全通用的代码片段,可以反转任何NSMutableArray:
/* Algorithm: swap the object N elements from the top with the object N * elements from the bottom. Integer division will wrap down, leaving * the middle element untouched if count is odd. */ for(int i = 0; i < [array count] / 2; i++) { int j = [array count] - i - 1; [array exchangeObjectAtIndex:i withObjectAtIndex:j]; }
您可以将其包装在C函数中,或者对于奖励积分,使用类别将其添加到NSMutableArray.(在这种情况下,'array'将变成'self'.)你也可以通过[array count]
在循环之前分配变量并使用该变量来优化它,如果你愿意的话.
如果你只有一个常规的NSArray,就无法在适当的位置反转它,因为无法修改NSArrays.但你可以制作一个反向副本:
NSMutableArray * copy = [NSMutableArray arrayWithCapacity:[array count]]; for(int i = 0; i < [array count]; i++) { [copy addObject:[array objectAtIndex:[array count] - i - 1]]; }
或者使用这个小技巧在一行中完成:
NSArray * copy = [[array reverseObjectEnumerator] allObjects];
如果您只想向后循环数组,可以使用for
/ in
loop [array reverseObjectEnumerator]
,但使用它可能更有效-enumerateObjectsWithOptions:usingBlock:
:
[array enumerateObjectsWithOptions:NSEnumerationReverse usingBlock:^(id obj, NSUInteger idx, BOOL *stop) { // This is your loop body. Use the object in obj here. // If you need the index, it's in idx. // (This is the best feature of this method, IMHO.) // Instead of using 'continue', use 'return'. // Instead of using 'break', set '*stop = YES' and then 'return'. // Making the surrounding method/block return is tricky and probably // requires a '__block' variable. // (This is the worst feature of this method, IMHO.) }];
(注意:2014年基本更新,基础知识经验还有5年,新的Objective-C功能或两个,以及评论中的一些提示.)
在回顾了上面的其他答案之后,在这里找到了Matt Gallagher的讨论
我建议这个:
NSMutableArray * reverseArray = [NSMutableArray arrayWithCapacity:[myArray count]]; for (id element in [myArray reverseObjectEnumerator]) { [reverseArray addObject:element]; }
正如马特所说:
在上面的例子中,您可能想知道 - [NSArray reverseObjectEnumerator]是否会在循环的每次迭代中运行 - 可能会降低代码速度.<...>
此后不久,他回答如下:
<...>当for循环开始时,"collection"表达式仅被计算一次.这是最好的情况,因为您可以安全地在"集合"表达式中放置一个昂贵的函数,而不会影响循环的每次迭代性能.
GeorgSchö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
使用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);
NSMutableArray *objMyObject = [NSMutableArray arrayWithArray:[self reverseArray:objArrayToBeReversed]]; // Function reverseArray -(NSArray *) reverseArray : (NSArray *) myArray { return [[myArray reverseObjectEnumerator] allObjects]; }