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

问题描述 投票:1回答:2

我有两个字典作为[String: Any]类型的文本属性工作,并且为了切换ON或OFF所需的属性,我需要检查两个字典是否相同。

我尝试了以下方法:

let current = inputTextView.typingAttributes

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

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

二进制运算符'=='不能应用于两个'任意'操作数

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

谢谢

ios nsdictionary swift4
2个回答
0
投票

细节

  • Xcode版本10.2.1(10E1001),Swift 5

import Foundation

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 {
        if left.count != right.count { return false }
        for (index, leftValue) in left.enumerated() {
            guard areEqual(leftValue, right[index]) else { return false }
        }
        return true
    }

    static func != (left: [Element], right: [Element]) -> Bool {
        return !(left == right)
    }
}
extension Dictionary where Value: Any {

    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
    }

    static func != (left: [Key : Value], right: [Key : Value]) -> Bool {
        return !(left == right)
    }
}

Usage

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

Some tests

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": [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]])

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

test(dict1: ["key1":[1: "key1"] as [AnyHashable: Any]],
     dict2: ["key1":[1: "key1"] as [AnyHashable: String]])

test(dict1: ["key1":[1: "key1"] as [AnyHashable: Any]],
     dict2: ["key1":[2: "key1"] as [AnyHashable: String]])

测试结果

========================
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"): [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
========================
dict1: [AnyHashable("key1"): [1, 2, 3, 4]]
dict2: [AnyHashable("key1"): [1, 2, 3, 4]]
are equal
========================
dict1: [AnyHashable("key1"): [AnyHashable(1): "key1"]]
dict2: [AnyHashable("key1"): [AnyHashable(1): "key1"]]
are equal
========================
dict1: [AnyHashable("key1"): [AnyHashable(1): "key1"]]
dict2: [AnyHashable("key1"): [AnyHashable(2): "key1"]]
are not equal

2
投票

Any不符合Equatable协议。如果使用==算子,它是必须的类型。因此,您需要使用带有Any中提到的类型参数的函数来比较this answer对象:

func isEqual<T: Equatable>(type: T.Type, a: Any, b: Any) -> Bool? {
    guard let a = a as? T, let b = b as? T else { return nil }
    return a == b
}

但是,要使用此函数,您应该知道typingAttributes中每个值的确切类型。您可以使用Mirror结构实现此目的,如下所示:

let lilAny: Any = "What's my type? :("
print(Mirror(reflecting: lilAny).subjectType) // String
© www.soinside.com 2019 - 2024. All rights reserved.