Ios Swift 4对成员的引用模棱两可';jsonObject(带:选项:)&x27;

Ios Swift 4对成员的引用模棱两可';jsonObject(带:选项:)&x27;,ios,json,swift,json-serialization,Ios,Json,Swift,Json Serialization,尝试设置一个单元测试,该测试执行网络请求并尝试序列化响应。我当前收到错误:对成员“jsonObject(with:options:)”的引用不明确。。由于单元测试应该知道JSONSerialization是什么,所以对为什么会发生这种情况感到困惑。是吗 func testAccessKeys() { let expected = expectation(description: "Run the Access request") sut.request(.Access, data

尝试设置一个单元测试,该测试执行网络请求并尝试序列化响应。我当前收到错误:
对成员“jsonObject(with:options:)”的引用不明确。
。由于单元测试应该知道JSONSerialization是什么,所以对为什么会发生这种情况感到困惑。是吗

func testAccessKeys() {
    let expected = expectation(description: "Run the Access request")
    sut.request(.Access, data: nil) { finished, response in
        if response != nil && finished == true {
            guard let json = try? JSONSerialization.jsonObject(with: response!, options: .mutableContainers) as! [String:Any] else { return XCTFail("Access request was not a dictionary")}
            XCTAssertNotNil(json?["id"])
            expected.fulfill()
        } else {
            XCTFail("Access response was nil")
        }
    }
    waitForExpectations(timeout: 3) { error in
        if let error = error {
            XCTFail("Access request failure: \(error)")
        }
    }

}

确保响应类型为
Data
InputStream


正如您在

中看到的那样,如果您使用的是Swift 4,则最好使用新的
Codable
协议,并且
jsondeconder
classJSONSerialization在这种情况下应该可以工作,因为只有两个值会从此请求接收。只要响应是数据类型那么这应该行得通,它看起来是正确的语法。确保你在测试的顶部有导入的基础,你使用的是什么SWIFT版本?谢谢