我有2个词典:
let foo: [String: Any] = ["name": "John", "isHuman": true, "age": 20]
let bar: [String: Any] = ["name": "Ann", "isHuman": true, "age": 34]
我想在比较中排除某些键时进行比较。我想要排除的键位于另一个字典中:
let car: [String: Any] = ["age": 20]
我有这个扩展名:
extension Dictionary where Key: Equatable {
func isEqualTo(_ dictionary: Dictionary, excluding: [Key] = []) -> Bool {
let left = filter({ !excluding.contains($0.key) })
let right = dictionary.filter({ !excluding.contains($0.key) })
return NSDictionary(dictionary: left).isEqual(to: right)
}
}
但是当我调用它时,我需要将排除的密钥包装在Array
中,否则编译器将失败:
无法将“Dictionary.Keys”类型的值转换为预期的参数类型“[String]”
foo.isEqualTo(bar, excluding: Array(car.keys))
有效。
foo.isEqualTo(bar, excluding: car.keys)
会导致编译错误。
我是否可以调整我的扩展程序以避免将排除的密钥包装在Array
中?
答案 0 :(得分:4)
car.keys
的类型为Dictionary<Key, Value>.Keys
,即
集合而不是数组。
如果您将该功能声明为
func isEqualTo<C: Collection>(_ dictionary: Dictionary, excluding: C) -> Bool
where C.Element == Key
然后两者
foo.isEqualTo(bar, excluding: Array(car.keys))
foo.isEqualTo(bar, excluding: car.keys)
编译和工作。
答案 1 :(得分:2)
将excluding
声明为Keys
类型。
func isEqualTo(_ dictionary: Dictionary, excluding: Keys = [:].keys) -> Bool {
作为旁注,您可以通过将代码更新为:
来消除NSDictionary
的使用
let foo: [String: AnyHashable] = ["name": "John", "isHuman": true, "age": 20]
let bar: [String: AnyHashable] = ["name": "Ann", "isHuman": true, "age": 34]
let car: [String: Any] = ["age": 20, "name":"Bob"]
extension Dictionary where Key: Equatable, Value: Equatable {
func isEqualTo(_ dictionary: Dictionary, excluding: Keys = [:].keys) -> Bool {
let left = filter({ !excluding.contains($0.key) })
let right = dictionary.filter({ !excluding.contains($0.key) })
return left == right
}
}
请注意使用AnyHashable
代替Any
字段,并注意扩展中添加的, Value: Equatable
。