比较 Swift 4 中的两个 [String: Any] 字典

Iva*_*ino 6 nsdictionary ios swift4

我有两个字典作为文本属性类型工作[String: Any],为了打开或关闭所需的属性,我需要检查两个字典是否相同。

我尝试了以下方法:

let current = inputTextView.typingAttributes

let undo = current.elementsEqual(attributes, by: { (arg0, arg1) -> Bool in
    return ((arg0.key == arg1.key) && (arg0.value == arg1.value))
})
Run Code Online (Sandbox Code Playgroud)

但是在第二次评估时,我得到了错误:

二元运算符“==”不能应用于两个“任意”操作数

这里比较两个类型的字典的最佳方法是什么[String: Any]

谢谢

Vas*_*huk 8

细节

  • Xcode 版本 10.3 (10G8),Swift 5

解决方案

func areEqual (_ left: Any, _ right: Any) -> Bool {
    if  type(of: left) == type(of: right) &&
        String(describing: left) == String(describing: right) { return true }
    if let left = left as? [Any], let right = right as? [Any] { return left == right }
    if let left = left as? [AnyHashable: Any], let right = right as? [AnyHashable: Any] { return left == right }
    return false
}

extension Array where Element: Any {
    static func != (left: [Element], right: [Element]) -> Bool { return !(left == right) }
    static func == (left: [Element], right: [Element]) -> Bool {
        if left.count != right.count { return false }
        var right = right
        loop: for leftValue in left {
            for (rightIndex, rightValue) in right.enumerated() where areEqual(leftValue, rightValue) {
                right.remove(at: rightIndex)
                continue loop
            }
            return false
        }
        return true
    }
}
extension Dictionary where Value: Any {
    static func != (left: [Key : Value], right: [Key : Value]) -> Bool { return !(left == right) }
    static func == (left: [Key : Value], right: [Key : Value]) -> Bool {
        if left.count != right.count { return false }
        for element in left {
            guard   let rightValue = right[element.key],
                areEqual(rightValue, element.value) else { return false }
        }
        return true
    }
}
Run Code Online (Sandbox Code Playgroud)

用法

let comparisonResult = ["key1": 1, 2: "Value2"] == ["key1": ["key2":2]]     // false
print("!!!! \(comparisonResult)")
Run Code Online (Sandbox Code Playgroud)

一些测试

func test(dict1: [AnyHashable : Any], dict2:  [AnyHashable : Any]) {
    print("========================")
    print("dict1: \(dict1)")
    print("dict2: \(dict2)")
    print("are\(dict1 == dict2 ? " " : " not ")equal")
}

test(dict1: ["key1": 1, 2: "Value2"],
     dict2: ["key1": 1, 2: "Value2"])

test(dict1: ["key1": 1, 2: "Value2"],
     dict2: ["key1": 1])

test(dict1: [2: "Value2"],
     dict2: ["key1": 1])

test(dict1: ["1": 1],
     dict2: [1: 1])

test(dict1: [1: 2],
     dict2: [1: 3])

test(dict1: ["key1": [1,2,3,4]],
     dict2: ["key1": [1,2,3,4]])

test(dict1: ["key1": [1,2,3,4]],
     dict2: ["key1": [2,1,3,4]])

test(dict1: ["key1": [1,2,3,4]],
     dict2: ["key1": [2,1,3]])

test(dict1: ["key1": [1,2,3,4]],
     dict2: ["key1": [1,2,3,"4"]])

test(dict1: ["key1": ["key2":2]],
     dict2: ["key1": ["key2":2]])

test(dict1: ["key1": ["key2":2]],
     dict2: ["key1": ["key2":3]])

test(dict1: ["key1": ["key2":2]],
     dict2: ["key1": ["key2":3]])
Run Code Online (Sandbox Code Playgroud)

测试结果

========================
dict1: [AnyHashable("key1"): 1, AnyHashable(2): "Value2"]
dict2: [AnyHashable("key1"): 1, AnyHashable(2): "Value2"]
are equal
========================
dict1: [AnyHashable("key1"): 1, AnyHashable(2): "Value2"]
dict2: [AnyHashable("key1"): 1]
are not equal
========================
dict1: [AnyHashable(2): "Value2"]
dict2: [AnyHashable("key1"): 1]
are not equal
========================
dict1: [AnyHashable("1"): 1]
dict2: [AnyHashable(1): 1]
are not equal
========================
dict1: [AnyHashable(1): 2]
dict2: [AnyHashable(1): 3]
are not equal
========================
dict1: [AnyHashable("key1"): [1, 2, 3, 4]]
dict2: [AnyHashable("key1"): [1, 2, 3, 4]]
are equal
========================
dict1: [AnyHashable("key1"): [1, 2, 3, 4]]
dict2: [AnyHashable("key1"): [2, 1, 3, 4]]
are equal
========================
dict1: [AnyHashable("key1"): [1, 2, 3, 4]]
dict2: [AnyHashable("key1"): [2, 1, 3]]
are not equal
========================
dict1: [AnyHashable("key1"): [1, 2, 3, 4]]
dict2: [AnyHashable("key1"): [1, 2, 3, "4"]]
are not equal
========================
dict1: [AnyHashable("key1"): ["key2": 2]]
dict2: [AnyHashable("key1"): ["key2": 2]]
are equal
========================
dict1: [AnyHashable("key1"): ["key2": 2]]
dict2: [AnyHashable("key1"): ["key2": 3]]
are not equal
========================
dict1: [AnyHashable("key1"): ["key2": 2]]
dict2: [AnyHashable("key1"): ["key2": 3]]
are not equal
Run Code Online (Sandbox Code Playgroud)