我需要反转NSArray。
举个例子:
[1,2,3,4,5]必须变成:[5,4,3,2,1]
实现这一目标的最佳方式是什么?
我需要反转NSArray。
举个例子:
[1,2,3,4,5]必须变成:[5,4,3,2,1]
实现这一目标的最佳方式是什么?
当前回答
如果你想做的只是反向迭代,试试这个:
// iterate backwards
nextIndex = (currentIndex == 0) ? [myArray count] - 1 : (currentIndex - 1) % [myArray count];
你可以做一次[myArrayCount],并将其保存到一个局部变量(我认为这很昂贵),但我也猜测编译器将对上面写的代码做同样的事情。
其他回答
要更新这个,在Swift中它可以很容易地完成:
array.reverse()
有一个更简单的解决方案,如果你利用NSArray内置的reverseObjectEnumerator方法,以及NSEnumerator的allObjects方法:
NSArray* reversedArray = [[startArray reverseObjectEnumerator] allObjects];
allObjects被记录为返回一个数组,其中的对象还没有被nextObject遍历,顺序如下:
该数组按枚举顺序包含枚举器的所有剩余对象。
一些基准
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显然是赢家。
或者scala方式:
-(NSArray *)reverse
{
if ( self.count < 2 )
return self;
else
return [[self.tail reverse] concat:[NSArray arrayWithObject:self.head]];
}
-(id)head
{
return self.firstObject;
}
-(NSArray *)tail
{
if ( self.count > 1 )
return [self subarrayWithRange:NSMakeRange(1, self.count - 1)];
else
return @[];
}
有一个简单的方法。
NSArray *myArray = @[@"5",@"4",@"3",@"2",@"1"];
NSMutableArray *myNewArray = [[NSMutableArray alloc] init]; //this object is going to be your new array with inverse order.
for(int i=0; i<[myNewArray count]; i++){
[myNewArray insertObject:[myNewArray objectAtIndex:i] atIndex:0];
}
//other way to do it
for(NSString *eachValue in myArray){
[myNewArray insertObject:eachValue atIndex:0];
}
//in both cases your new array will look like this
NSLog(@"myNewArray: %@", myNewArray);
//[@"1",@"2",@"3",@"4",@"5"]
我希望这能有所帮助。