I have two collections:
我有两个集合:
let collection1:[String:[String:NSObject]] = ["somekey":["nestedkey":"value"]]
let collection2:[String:[String:NSObject]] = ["somekey":["nestedkey":"value"]]
//I would like to compare them using the following:
let collectionsAreEqual = collection1 == collection2
Copying and pasting the above code into a playground gives the following error:
将以上代码复制粘贴到一个操场上会产生以下错误:
I know I can write an equal function for this:
我知道我可以写出一个相等的函数
infix func == (this:[String:[String:NSObject]], that:[String:[String:NSObject]]){
//return true or false
}
In objective c, isEqual: on an NSDictionary handles this no problem, because it does the nested comparison for you. Is there some method of generally handling this in swift?
在objective c中,在NSDictionary上的isEqual可以处理这个问题,因为它为您做了嵌套比较。在swift中是否有一些一般的处理方法?
Update
更新
I can use the following:
我可以使用以下内容:
//:[String:[String:NSObject]]
let collection1:[String:NSObject] = ["somekey":["nestedkey":"value"]]
let collection2:[String:NSObject] = ["somekey":["nestedkey":"value"]]
//I would like to compare them using the following:
let collectionsAreEqual = collection1 == collection2
but it requires using NSObject as the value in the declaration. Is there a pure swift method to handle this?
但是它需要使用NSObject作为声明中的值。是否有一种快速的方法来处理这个问题?
2 个解决方案
#1
14
Here's an equality operator that will compare any two nested dictionaries with the same type:
下面是一个等式运算符,它将比较任意两个相同类型的嵌套字典:
func ==<T: Equatable, K1: Hashable, K2: Hashable>(lhs: [K1: [K2: T]], rhs: [K1: [K2: T]]) -> Bool {
if lhs.count != rhs.count { return false }
for (key, lhsub) in lhs {
if let rhsub = rhs[key] {
if lhsub != rhsub {
return false
}
} else {
return false
}
}
return true
}
#2
0
try this:
试试这个:
let collection1:[String:NSObject] = ["somekey":["nestedkey":"value"]]
let collection2:[String:NSObject] = ["somekey":["nestedkey":"value"]]
let collectionsAreEqual = ((collection1 as NSDictionary).isEqual(collection2 as NSDictionary)
By casting the swift objects to Foundation objects they get the equality operator. They will call the equality operator on every element recursively, so there you have your deep comparison.
通过将swift对象转换为基础对象,它们得到等式运算符。它们会递归地调用每个元素上的等式运算符,这样就有了深入的比较。
#1
14
Here's an equality operator that will compare any two nested dictionaries with the same type:
下面是一个等式运算符,它将比较任意两个相同类型的嵌套字典:
func ==<T: Equatable, K1: Hashable, K2: Hashable>(lhs: [K1: [K2: T]], rhs: [K1: [K2: T]]) -> Bool {
if lhs.count != rhs.count { return false }
for (key, lhsub) in lhs {
if let rhsub = rhs[key] {
if lhsub != rhsub {
return false
}
} else {
return false
}
}
return true
}
#2
0
try this:
试试这个:
let collection1:[String:NSObject] = ["somekey":["nestedkey":"value"]]
let collection2:[String:NSObject] = ["somekey":["nestedkey":"value"]]
let collectionsAreEqual = ((collection1 as NSDictionary).isEqual(collection2 as NSDictionary)
By casting the swift objects to Foundation objects they get the equality operator. They will call the equality operator on every element recursively, so there you have your deep comparison.
通过将swift对象转换为基础对象,它们得到等式运算符。它们会递归地调用每个元素上的等式运算符,这样就有了深入的比较。