如何比较 Swift 中的两个词典?
How do I compare two dictionaries in Swift?
有没有简单的方法来比较 swift 中的两个 [String: AnyObject]
词典,因为它不接受 ==
运算符?
通过比较两个词典,我的意思是检查它们是否具有完全相同的键以及每个键是否具有相同的值。
在Swift2中,当Key
和Value
都是Equatable
时,可以用==
字典本身:
public func ==<Key : Equatable, Value : Equatable>(lhs: [Key : Value], rhs: [Key : Value]) -> Bool
而且,NSObject 是可等价的:
public func ==(lhs: NSObject, rhs: NSObject) -> Bool
在您的情况下,如果您正在处理要使用 isEqual:
进行比较的 Obj-C 对象,您可以简单地使用 NSObject 作为您的值类型(而不是 AnyObject)。
正如 Hot Licks 已经提到的,您可以使用 NSDictionary 方法 isEqualToDictionary() 来检查它们是否相等,如下所示:
let dic1: [String: AnyObject] = ["key1": 100, "key2": 200]
let dic2: [String: AnyObject] = ["key1": 100, "key2": 200]
let dic3: [String: AnyObject] = ["key1": 100, "key2": 250]
println( NSDictionary(dictionary: dic1).isEqualToDictionary(dic2) ) // true
println( NSDictionary(dictionary: dic1).isEqualToDictionary(dic3) ) // false
您还可以实现自定义运算符“==”,如下所示:
public func ==(lhs: [String: AnyObject], rhs: [String: AnyObject] ) -> Bool {
return NSDictionary(dictionary: lhs).isEqualToDictionary(rhs)
}
println(dic1 == dic2) // true
println(dic1 == dic3) // false
Xcode 9 • Swift 4
根据文档,字典现在定义为结构:
struct Dictionary<Key : Hashable, Value> : Collection, ExpressibleByDictionaryLiteral
Description
A collection whose elements are key-value pairs. A
dictionary is a type of hash table, providing fast access to the
entries it contains. Each entry in the table is identified using its
key, which is a hashable type such as a string or number. You use that
key to retrieve the corresponding value, which can be any object. In
other languages, similar data types are known as hashes or associated
arrays. Create a new dictionary by using a dictionary literal. A
dictionary literal is a comma-separated list of key-value pairs, in
which a colon separates each key from its associated value, surrounded
by square brackets. You can assign a dictionary literal to a variable
or constant or pass it to a function that expects a dictionary.
以下是创建 HTTP 响应代码及其相关消息的字典的方法:
var responseMessages = [200: "OK",
403: "Access forbidden",
404: "File not found",
500: "Internal server error"]
The responseMessages variable is inferred to have type [Int: String]
.
The Key type of the dictionary is Int
, and the Value type of the
dictionary is String
.
要创建没有键值对的字典,请使用空字典文字 ([:])。
var emptyDict: [String: String] = [:]
任何符合Hashable协议的类型都可以作为字典的Key类型,包括Swift的所有基本类型。您可以使用自己的自定义类型作为字典键,方法是使它们符合 Hashable 协议。
我们不再需要定义自定义运算符:
来自文档:
static func ==(lhs: [Key : Value], rhs: [Key : Value]) -> Bool
测试:
let dic1 = ["key1": 100, "key2": 200]
let dic2 = ["key1": 100, "key2": 200]
let dic3 = ["key1": 100, "key2": 250]
print(dic1 == dic2) // true
print(dic1 == dic3) // false
在上面的示例中,所有字典键和值都是同一类型。
如果我们尝试比较两个 [String: Any]
类型的字典,Xcode 会抱怨二元运算符 == 不能应用于两个 [String: Any]
操作数。
let dic4: [String: Any] = ["key1": 100, "key2": "200"]
let dic5: [String: Any] = ["key1": 100, "key2": "200"]
let dic6: [String: Any] = ["key1": 100, "key2": Date()]
print(dic4 == dic5) // Binary operator == cannot be applied to two `[String: Any]` operands
但我们可以扩展 ==
运算符功能实现中缀运算符,将 Swift 字典转换为 NSDictionary 并将字典值限制为 Hashable Protocol:
Xcode 11 • Swift 5.1
public func ==<K, L: Hashable, R: Hashable>(lhs: [K: L], rhs: [K: R] ) -> Bool {
(lhs as NSDictionary).isEqual(to: rhs)
}
测试:
let dic4: [String: AnyHashable] = ["key1": 100, "key2": "200"]
let dic5: [String: AnyHashable] = ["key1": 100, "key2": "200"]
let dic6: [String: AnyHashable] = ["key1": 100, "key2": Date()]
print(dic4 == dic5) // true
print(dic4 == dic6) // false
let dic7: [String: String] = [ "key2": "200"]
let dic8: [String: Date] = [ "key2": Date()]
print(dic7 == dic8) // false
如果字典的值没有自定义类型,在 Swift 2+ 中,您可以使用 ==
运算符比较两个 Dictionary
以检查它们是否相等。
但在某些情况下使用自定义类型作为 Dictionary
的值(如 struct
),您必须采用 Equatable
以便该自定义类型使用 ==
运算符。
例如:
// custom type
struct Custom: Equatable {
var value: Int
}
// MARK: adopting Equatable
func ==(lhs: Custom, rhs: Custom) -> Bool {
if lhs.value == rhs.value {
return true
} else {
return false
}
}
现在您可以使用 ==
运算符来比较两个字典:
let dic3: [String: Custom] = ["key1": Custom(value:1), "key2": Custom(value:2)]
let dic4: [String: Custom] = ["key1": Custom(value:1), "key2": Custom(value:2)]
if (dic3 == dic4) {
print("equal")
} else {
print("not equal")
}
Swift 4 次更新:
比较词典现在是原生的! (文档 here)
Swift 3:
Leo Dabus 已经有一个写得很好的 post 和公认的解决方案。然而,对我来说,我发现它还需要一个步骤才能完全可用。从他的代码中可以看出,您需要将字典类型设置为 [AnyHashable: Any]
,否则您将得到 Binary operator '==' cannot be applied to two '[String : Any]' operands
,以在我的示例中使用反序列化 JSON 中常用的字典。
泛型来拯救!:
// Swift 3.0
func == <K, V>(left: [K:V], right: [K:V]) -> Bool {
return NSDictionary(dictionary: left).isEqual(to: right)
}
或者在另一种情况下,[String: Any?]
:
func == <K, V>(left: [K:V?], right: [K:V?]) -> Bool {
guard let left = left as? [K: V], let right = right as? [K: V] else { return false }
return NSDictionary(dictionary: left).isEqual(to: right)
}
有没有简单的方法来比较 swift 中的两个 [String: AnyObject]
词典,因为它不接受 ==
运算符?
通过比较两个词典,我的意思是检查它们是否具有完全相同的键以及每个键是否具有相同的值。
在Swift2中,当Key
和Value
都是Equatable
时,可以用==
字典本身:
public func ==<Key : Equatable, Value : Equatable>(lhs: [Key : Value], rhs: [Key : Value]) -> Bool
而且,NSObject 是可等价的:
public func ==(lhs: NSObject, rhs: NSObject) -> Bool
在您的情况下,如果您正在处理要使用 isEqual:
进行比较的 Obj-C 对象,您可以简单地使用 NSObject 作为您的值类型(而不是 AnyObject)。
正如 Hot Licks 已经提到的,您可以使用 NSDictionary 方法 isEqualToDictionary() 来检查它们是否相等,如下所示:
let dic1: [String: AnyObject] = ["key1": 100, "key2": 200]
let dic2: [String: AnyObject] = ["key1": 100, "key2": 200]
let dic3: [String: AnyObject] = ["key1": 100, "key2": 250]
println( NSDictionary(dictionary: dic1).isEqualToDictionary(dic2) ) // true
println( NSDictionary(dictionary: dic1).isEqualToDictionary(dic3) ) // false
您还可以实现自定义运算符“==”,如下所示:
public func ==(lhs: [String: AnyObject], rhs: [String: AnyObject] ) -> Bool {
return NSDictionary(dictionary: lhs).isEqualToDictionary(rhs)
}
println(dic1 == dic2) // true
println(dic1 == dic3) // false
Xcode 9 • Swift 4
根据文档,字典现在定义为结构:
struct Dictionary<Key : Hashable, Value> : Collection, ExpressibleByDictionaryLiteral
Description
A collection whose elements are key-value pairs. A dictionary is a type of hash table, providing fast access to the entries it contains. Each entry in the table is identified using its key, which is a hashable type such as a string or number. You use that key to retrieve the corresponding value, which can be any object. In other languages, similar data types are known as hashes or associated arrays. Create a new dictionary by using a dictionary literal. A dictionary literal is a comma-separated list of key-value pairs, in which a colon separates each key from its associated value, surrounded by square brackets. You can assign a dictionary literal to a variable or constant or pass it to a function that expects a dictionary.
以下是创建 HTTP 响应代码及其相关消息的字典的方法:
var responseMessages = [200: "OK",
403: "Access forbidden",
404: "File not found",
500: "Internal server error"]
The responseMessages variable is inferred to have type
[Int: String]
. The Key type of the dictionary isInt
, and the Value type of the dictionary isString
.
要创建没有键值对的字典,请使用空字典文字 ([:])。
var emptyDict: [String: String] = [:]
任何符合Hashable协议的类型都可以作为字典的Key类型,包括Swift的所有基本类型。您可以使用自己的自定义类型作为字典键,方法是使它们符合 Hashable 协议。
我们不再需要定义自定义运算符:
来自文档:
static func ==(lhs: [Key : Value], rhs: [Key : Value]) -> Bool
测试:
let dic1 = ["key1": 100, "key2": 200]
let dic2 = ["key1": 100, "key2": 200]
let dic3 = ["key1": 100, "key2": 250]
print(dic1 == dic2) // true
print(dic1 == dic3) // false
在上面的示例中,所有字典键和值都是同一类型。
如果我们尝试比较两个 [String: Any]
类型的字典,Xcode 会抱怨二元运算符 == 不能应用于两个 [String: Any]
操作数。
let dic4: [String: Any] = ["key1": 100, "key2": "200"]
let dic5: [String: Any] = ["key1": 100, "key2": "200"]
let dic6: [String: Any] = ["key1": 100, "key2": Date()]
print(dic4 == dic5) // Binary operator == cannot be applied to two `[String: Any]` operands
但我们可以扩展 ==
运算符功能实现中缀运算符,将 Swift 字典转换为 NSDictionary 并将字典值限制为 Hashable Protocol:
Xcode 11 • Swift 5.1
public func ==<K, L: Hashable, R: Hashable>(lhs: [K: L], rhs: [K: R] ) -> Bool {
(lhs as NSDictionary).isEqual(to: rhs)
}
测试:
let dic4: [String: AnyHashable] = ["key1": 100, "key2": "200"]
let dic5: [String: AnyHashable] = ["key1": 100, "key2": "200"]
let dic6: [String: AnyHashable] = ["key1": 100, "key2": Date()]
print(dic4 == dic5) // true
print(dic4 == dic6) // false
let dic7: [String: String] = [ "key2": "200"]
let dic8: [String: Date] = [ "key2": Date()]
print(dic7 == dic8) // false
如果字典的值没有自定义类型,在 Swift 2+ 中,您可以使用 ==
运算符比较两个 Dictionary
以检查它们是否相等。
但在某些情况下使用自定义类型作为 Dictionary
的值(如 struct
),您必须采用 Equatable
以便该自定义类型使用 ==
运算符。
例如:
// custom type
struct Custom: Equatable {
var value: Int
}
// MARK: adopting Equatable
func ==(lhs: Custom, rhs: Custom) -> Bool {
if lhs.value == rhs.value {
return true
} else {
return false
}
}
现在您可以使用 ==
运算符来比较两个字典:
let dic3: [String: Custom] = ["key1": Custom(value:1), "key2": Custom(value:2)]
let dic4: [String: Custom] = ["key1": Custom(value:1), "key2": Custom(value:2)]
if (dic3 == dic4) {
print("equal")
} else {
print("not equal")
}
Swift 4 次更新:
比较词典现在是原生的! (文档 here)
Swift 3:
Leo Dabus 已经有一个写得很好的 post 和公认的解决方案。然而,对我来说,我发现它还需要一个步骤才能完全可用。从他的代码中可以看出,您需要将字典类型设置为 [AnyHashable: Any]
,否则您将得到 Binary operator '==' cannot be applied to two '[String : Any]' operands
,以在我的示例中使用反序列化 JSON 中常用的字典。
泛型来拯救!:
// Swift 3.0
func == <K, V>(left: [K:V], right: [K:V]) -> Bool {
return NSDictionary(dictionary: left).isEqual(to: right)
}
或者在另一种情况下,[String: Any?]
:
func == <K, V>(left: [K:V?], right: [K:V?]) -> Bool {
guard let left = left as? [K: V], let right = right as? [K: V] else { return false }
return NSDictionary(dictionary: left).isEqual(to: right)
}