如何在objective-c中合并2个NSSets?

时间:2011-08-12 12:16:43

标签: cocoa nsset

如何在objective-c中合并2个NSSets?

我无法在谷歌找到解决方案。

3 个答案:

答案 0 :(得分:32)

在NSSet的方法中很容易发现:

- (NSSet *) setByAddingObjectsFromSet:(NSSet*) other;

答案 1 :(得分:5)

如果其中一个集合是NSMutableSet,那么您可以使用联合操作,如下例所示:

// Create / Get the sets
NSMutableSet *firstSet = [NSMutableSet setWithArray:@[@"1", @"2"]];
NSSet *secondSet = [NSSet setWithArray:@[@"3",@"4"]];

// Add missing values from the second set to the first set
[firstSet unionSet:secondSet];

答案 2 :(得分:1)

如果要合并两组,可以使用此方法。

NSSet *mergedSet = [set setByAddingObjectsFromSet:set];

如果要将数组合并到集合中,则可以使用

NSSet *mergedSet = [set setByAddingObjectsFromArray:array];