14

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

我在谷歌上找不到解决方案。

4

3 回答 3

32

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

- (NSSet *) setByAddingObjectsFromSet:(NSSet*) other;
于 2011-08-12T12:27:15.890 回答
5

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

// 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];
于 2014-05-25T09:07:04.800 回答
2

如果要合并两个集合,则可以使用它。

NSSet *mergedSet = [set setByAddingObjectsFromSet:set];

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

NSSet *mergedSet = [set setByAddingObjectsFromArray:array];
于 2013-12-09T06:47:50.677 回答