如何将一个NSArray的string变成一个唯一的string数组,按照相同的顺序?

如果你有一个NSArray的string

{ @"ONE", @"ONE", @"ONE", "TWO", @"THREE", @"THREE" } 

我将如何把它变成

 { @"ONE", @"TWO", @"THREE" } 

..其中数组遵循原来的相同的顺序。 我认为你可以把一个数组变成一个NSSet来获取唯一的项目,但是如果你把它变成一个数组,你不能保证得到相同的顺序。

我最初的想法是,你可以这样做:

 NSArray * a = [NSArray arrayWithObjects:@"ONE", @"ONE", @"ONE", @"TWO", @"THREE", @"THREE", nil]; NSLog(@"%@", [a valueForKeyPath:@"@distinctUnionOfObjects.self"]); 

但是这并不能维持秩序。 因此,您必须手动执行此操作:

 NSArray * a = [NSArray arrayWithObjects:@"ONE", @"ONE", @"ONE", @"TWO", @"THREE", @"THREE", nil]; NSMutableArray * unique = [NSMutableArray array]; NSMutableSet * processed = [NSMutableSet set]; for (NSString * string in a) { if ([processed containsObject:string] == NO) { [unique addObject:string]; [processed addObject:string]; } } 

我使用一个NSMutableSet来确定我之前是否已经遇到了这个入口(与[unique containsObject:string]相反,因为一个集合有O(1)查找时间,而一个数组有O(n)查找时间。如果你只处理less量的对象,那么这个并不重要,但是如果源数组非常大,那么使用这个集合来确定唯一性可能会增加一点速度(但是,你应该使用工具来分析你的代码,看看是否有必要)

你可以这样做:

 NSArray * uniqueArray = [[NSOrderedSet orderedSetWithArray:duplicatesArray] array]; 

这样,你也保存了订单!

我想你可以这样做

 NSArray * uniqueArray = [[Yourarray valueForKeyPath:@"@distinctUnionOfObjects.self"] sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)]; 

我希望这会帮助你

嗯..你可以只使用一个循环?

 NSMutableArray *newarray = [[NSMutableArray alloc] init]; NSString *laststring = nil; for (NSString *currentstring in oldarray) { if (![currentstring isEqualtoString:laststring]) [newarray addObject:currentstring]; laststring = currentstring } 

下面是一个很好的类,它定义了一个像@distinctUnionOfObjects这样的自定义操作符 ,除了它只能用于string,它将保持原来的顺序。 注意:它不会为您sortingstring。 它只保留任何string重复的第一个实例。

用法:

 #import "NSArray+orderedDistinctUnionOfStrings.h" ... // if you feed it an array that has already been ordered, it will work as expected NSArray *myArray = @[@"ONE", @"ONE", @"ONE", @"TWO", @"THREE", @"THREE"]; NSArray *myUniqueArray = [myArray valueForKeyPath:@"@orderedDistinctUnionOfStrings.self"]; 

输出:

 myUniqueArray = ( "ONE", "TWO", "THREE" ) 

.h文件:

 #import <Foundation/Foundation.h> @interface NSArray (orderedDistinctUnionOfStrings) @end 

.m文件:

 #import "NSArray+orderedDistinctUnionOfObjects.h" @implementation NSArray (orderedDistinctUnionOfObjects) - (id) _orderedDistinctUnionOfStringsForKeyPath:(NSString*)keyPath { NSMutableIndexSet *removeIndexes = [NSMutableIndexSet indexSet]; for (NSUInteger i = 0, n = self.count; i < n; ++i) { if ([removeIndexes containsIndex:i]) { continue; } NSString *str1 = [[self objectAtIndex:i] valueForKeyPath:keyPath]; for (NSUInteger j = i+1; j < n; ++j) { if ([removeIndexes containsIndex:j]) { continue; } id obj = [self objectAtIndex:j]; NSString *str2 = [obj valueForKeyPath:keyPath]; if ([str1 isEqualToString:str2]) { [removeIndexes addIndex:j]; } } } NSMutableArray *myMutableCopy = [self mutableCopy]; [myMutableCopy removeObjectsAtIndexes:removeIndexes]; return [[NSArray arrayWithArray:myMutableCopy] valueForKeyPath:[NSString stringWithFormat:@"@unionOfObjects.%@", keyPath]]; } @end 

这里是一个很好的阅读如何生成自己的运营商,神秘(稍微)这是如何工作的: http : //bou.io/KVCCustomOperators.html