Swift 4对成员'jsonObject(带:options:)'

时间:2018-04-09 09:43:29

标签: ios json swift json-serialization

尝试设置执行网络请求并尝试序列化响应的单元测试。我目前收到错误:Ambiguous reference to member '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)")
        }
    }

}

1 个答案:

答案 0 :(得分:0)

确保回复的类型为DataInputStream

这些是此功能接受的唯一类型,您可以在documentation

中看到