ios - Swift 4 对成员 'jsonObject(with:options:)' 的引用不明确

标签 ios json swift json-serialization

尝试设置执行网络请求并尝试序列化响应的单元测试。我当前收到错误:对成员“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)")
        }
    }

}

最佳答案

确保响应的类型为DataInputStream

这些是此函数唯一接受的类型,如 documentation 中所示。

关于ios - Swift 4 对成员 'jsonObject(with:options:)' 的引用不明确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49729953/

相关文章:

ios - 自定义 segue,扩展源 View Controller

ios - 在 Swift 中将 NSIndexPath 转换为 NSInteger

ios - 当新的 View Controller 被激活时,变量数据被删除

iOS 后台模式位置更新 - 管理器在后台模式下不更新

ios - 向以编程方式创建的 UIButton 添加功能

php - 如何从这个多维数组形成 JSON

ios - 为什么 Google Maps iOS SDK 会使我的应用程序崩溃?

json - 使用BASH从json文件中获取单行键值对作为mongodb备份脚本

java - 在 Java 中解析 JSON 3 级深度

ios - 我想知道如何将结果值传递给另一个 View Controller 。