如何从数组中获取唯一值
请注意 这是用于OSX,而不是iOS.
Please note this is for OSX, not for iOS.
我已经查看并尝试了其他SO问题中的一些解决方案,但是似乎没有一个对我有用,因此:
I have looked and tried some solutions in other SO questions, but none seem to work for me, hence this:
我想从数组中获得一组独特的年份.
I want to get a unique set of years out of an array.
我的代码是这样的:
NSMutableArray * unique = [NSMutableArray array];
NSMutableSet * processed = [NSMutableSet set];
for (yearString in yearStringArray) {
if (![processed containsObject:yearString] == NO) {
[unique addObject:yearString];
}
[processed addObject:yearString];
}
NSLog(@"unique: %@",unique );
NSLog(@"processed: %@",processed );
}];
The console returns:
unique: (
(
1941,
1942,
1943,
1945,
1948,
1948,
1948,
1949
)
processed: {(
(
1941,
1942,
1943,
1945,
1948,
1948,
1948,
1949
)
)}
我只想要一个1948年.我将不胜感激.
I only want one 1948. I would appreciate any help.
更新:感谢您的回答.请让他们来!!!
UPDATES: Thanks for the answers all. Keep em coming please!!!
这些是我现在尝试过的代码片段:
These are the code snips I've tried now:
1.
for (yearString in yearStringArray) {
if ([processed containsObject:yearString] == NO) {
[unique addObject:yearString];
}
[processed addObject:yearString];
}
2.
NSString *yearString = [[NSString alloc] init];
NSArray *objectArray = [[NSArray alloc] init];
[objectArray = objects copy];
for (int j = 0; j <= objectArray.count; j++) {
NSString *yearString = [objectArray valueForKey:@"year"];
[yearStringArray addObject:yearString];
}
NSMutableArray * unique = [NSMutableArray array];
NSMutableSet * processed = [NSMutableSet set];
for (yearString in yearStringArray) {
[unique addObject:yearString];
[processed addObject:yearString];
}
NSArray *processed2 = [[NSSet setWithArray:unique] allObjects];
NSLog(@"unique: %@",unique );
NSLog(@"processed: %@",processed );
NSLog(@"processed2: %@",processed2 );
//[self setYears];
}];
3.
NSString *yearString = [[NSString alloc] init];
NSArray *objectArray = [[NSArray alloc] init];
[objectArray = objects copy];
for (int j = 0; j <= objectArray.count; j++) {
yearString = [objectArray valueForKey:@"year"];
[yearStringArray addObject:yearString];
}
NSArray *uniqueYears = [yearStringArray valueForKeyPath:@"@distinctUnionOfObjects.self"];
NSLog(@"uniqueYears: %@", uniqueYears);
}];
}
仍然没有独特的年份.
一种简单的方法是从具有重复项的数组中创建一个NSSet
.结果是一个集合,根据定义,该集合仅存储唯一对象.然后使用NSSet的-allObjects
方法将NSSet
转换回NSArray
.唯一的缺点是,通过转换为NSSet
,您将失去原始顺序.
An simple way is to create an NSSet
from the array with duplicates. The result is a set which by definition stores only the unique objects. Then using NSSet's -allObjects
method to convert the NSSet
back to an NSArray
. The only downside is you lose the original ordering by converting to an NSSet
.
NSArray *uniqueArray = [[NSSet setWithArray:duplicateArray] allObjects];
如果您需要保留顺序并且可能需要10.7+,则可以使用NSOrderedSet
.
If you need to preserve the ordering and can require 10.7+, you can use an NSOrderedSet
.
NSArray *uniqueArray = [[NSOrderedSet orderedSetWithArray:duplicateArray] array];
感谢WWDC 2013 会议228-可可和可可粉中的隐藏宝石马特·汤普森/em>, 没有创建中间集的另一种方法.
Thanks to Mattt Thompson in the WWDC 2013 Session 228 - Hidden Gems in Cocoa and Cocoa Touch, there is another way without creating an intermediate set.
NSArray *uniqueArray = [duplicateArray valueForKeyPath:@"@distinctUnionOfObjects.self"]