我有两个集合:
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
Run Code Online (Sandbox Code Playgroud)
将上述代码复制并粘贴到操场中会出现以下错误:
我知道我可以为此写一个相同的功能:
infix func == (this:[String:[String:NSObject]], that:[String:[String:NSObject]]){
//return true or false
}
Run Code Online (Sandbox Code Playgroud)
在目标c中,isEqual:在NSDictionary上处理这个没问题,因为它为你做了嵌套比较.是否有一些通常在swift中处理这个的方法?
更新
我可以使用以下内容:
//:[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
Run Code Online (Sandbox Code Playgroud)
但它需要使用NSObject作为声明中的值.有没有一个纯粹的快速方法来处理这个?
Nat*_*ook 14
这是一个相等运算符,它将比较任何两个具有相同类型的嵌套字典:
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
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
3694 次 |
最近记录: |