是的,Swift有Set类。
let array1 = ["a","b","c"] let array2 = ["a","d"] let set1:Set<String> = Set(array1) let set2:Set<String> = Set(array2)
Swift 3.0可以对集合执行以下操作:
firstSet.union(secondSet)// Union of two sets firstSet.intersection(secondSet)// Intersection of two sets firstSet.symmetricDifference(secondSet)// exclusiveOr
Swift 2.0可以计算数组参数:
set1.union(array2) // {"a","c","d"} set1.intersect(array2) // {"a","b"} set1.subtract(array2) // {"c"} set1.exclusiveOr(array2) // {"c","d"}
Swift 1.2可以计算:
set1.union(set2) // {"a","d"} set1.intersect(set2) // {"a","b"} set1.subtract(set2) // {"c"} set1.exclusiveOr(set2) // {"c","d"}
您可以在文档部分找到更多示例。
感谢Michael Stern在Swift 2.0更新的评论中。