我有两个来自Web服务的数组,我需要找出Array2是否与Array1具有相同的对象。 所以,为此我使用下面的代码:
{{1}}
但它在上面的行“Array(Set(arr3))”上显示错误 错误是: - 为Set
添加值答案 0 :(得分:0)
试试这个:
var arr1 = ["A","B","C"]
var arr2 = ["A","B","C"]
if Set(arr1).symmetricDifference(arr2).isEmpty {
print("The Arrays Match")
}
答案 1 :(得分:0)
Hashable
protocol
和间接Equatable
protocol
。struct
的示例,您也可以使用类。struct CustomObject : Hashable{
var something : Int //It is just an example, this could be any type, but some how you should find a way to compute the hash value.
//MARK: Hashable
var hashValue: Int {
return something
}
}
//MARK: CustomObject - Equatable
func ==(lhs: CustomObject, rhs: CustomObject) -> Bool {
return lhs.something == rhs.something
}