找不到接受类型参数列表的 XCTAssertEqual 的重载 ([String : AnyObject], [String : AnyObject])

Cannot find an overload for XCTAssertEqual that accepts an argument list of type ([String : AnyObject], [String : AnyObject])

我有一个方法:

func tableAsDictionary() -> [String: AnyObject]

然后我需要测试这个:

let tableDictionary = table.tableAsDictionary()

let expectedDictionary: [String: AnyObject] = [
    "id": "1234",
    "name": "Next to window",
    "number": 23
]

XCTAssertEqual(tableDictionary, expectedDictionary) //error

Cannot find an overload for XCTAssertEqual that accepts an argument list of type [String : AnyObject], [String : AnyObject]

问题是字典的 == 运算符要求 键和值类型都是 Equatable:

func ==<Key : Equatable, Value : Equatable>(lhs: [Key : Value], rhs: [Key : Value]) -> Bool

但是AnyObject不符合Equatable.

一个简单的修复方法是替换字典类型 [String: AnyObject] 通过 [String : NSObject],那么您的代码编译没有问题。