我有两个集合,并使用isSubsetOfSet:来确定接收集合是否全部存在于另一个集合中。但是,我需要找出isSubsetOfSet:失败时接收集中有多少个唯一条目。例如:
NSSet *set1 = [NSSet setWithObjects:@"1", @"2", @"3"];
NSSet *set2 = [NSSet setWithObjects:@"1", @"3", @"4", @"5"];
if (![set1 isSubsetOfSet:set2]) {
How many items are in set1 that are not in set2? The answer should be 1. (a string "2")
}
任何帮助将不胜感激。谢谢。
最佳答案
您可以复制set1
,然后从中减去set2
,如下所示:
NSMutableSet *missing = [NSMutableSet setWithSet:set1];
[missing minusSet:set2];
现在
missing
包含set1
中缺少的set2
中的所有对象。您可以跳过对isSubsetOfSet:
的调用,而是将missing.length
与零进行比较。关于ios - NSSet与第二组相比计数唯一条目,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/28601026/