简体   繁体   English

无法将类型'(Data?)->()'的值转换为预期的参数类型'(_)->()'

[英]Cannot convert value of type '(Data?) -> ()' to expected argument type '(_) -> ()'

I created a function to retrieve json data from a network server. 我创建了一个从网络服务器检索json数据的函数。 It works properly when run in a production environment but when it's setup to be unit-tested the error : Cannot convert value of type '(Data?) -> ()' to expected argument type '(_) -> ()' . 它在生产环境中运行时可以正常工作,但是将其设置为进行单元测试时出现错误: Cannot convert value of type '(Data?) -> ()' to expected argument type '(_) -> ()' Here is the function: 这是函数:

func getJSONData <T : Codable> (url: ServerUrl, type: T.Type, completionHandler:@escaping (_ details: T) -> ())  {

    guard let url = URL(string: baseURL + url.rawValue) else { return }


    session.dataTask(with: url, completionHandler: {
        (data, response, err) in

        guard err == nil else {
        print("There was a network error:", err as Any)
        return
        }
        guard let data = data else { return }

        do {

            let updatedModels = try JSONDecoder().decode(T.self, from: data)
            completionHandler(updatedModels)

        } catch let jsonErr {
            print("Error serializing json:", jsonErr)
            return }

        }).resume()
}

Here it is in the unit test: 这是在单元测试中:

func test_getJSONData_should_return_data() {

var actualData: Data?

sut.getJSONData(url: ServerUrl.colors, type: MainColor.self, completionHandler: { (data) in //error here

    actualData = data

})
XCTAssertNotNil(actualData)

} }

问题是MainColor返回类型MainColor值不是Data ,这应该是

var actualData: MainColor?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法将“数据”类型的值转换为预期的参数类型“数据” - cannot convert value of type 'Data' to expected argument type 'Data' Swift 4:无法将“数据”类型的值转换为预期的参数类型“数据” - Swift 4 : Cannot convert value of type 'Data' to expected argument type 'Data' 无法将类型&#39;()&#39;的值转换为预期的参数&#39;()-&gt; void&#39; - cannot convert value of type '()' to expected argument '() -> void' 无法将类型的值转换为预期的参数Firebase - Cannot convert value of type to expected argument Firebase 无法转换期望参数类型GTLServiceCompletionHandler的值 - Cannot convert value of expected argument type GTLServiceCompletionHandler 无法将类型&#39;String?.Type&#39;的值转换为预期的参数类型&#39;String?&#39; - Cannot convert value of type 'String?.Type' to expected argument type 'String?' 无法将“(QueryDocumentSnapshot).Type”类型的值转换为预期的参数类型“QueryDocumentSnapshot” - cannot convert value of type '(QueryDocumentSnapshot).Type' to expected argument type 'QueryDocumentSnapshot' 无法将类型“ [[ModelName]?]”的值转换为预期的参数类型“ [ModelName] .Type” - Cannot convert value of type '[[ModelName]?]' to expected argument type '[ModelName].Type' 无法将 [Type] 类型的值转换为预期的参数类型“some View” - Cannot convert value of type [Type] to expected argument type 'some View' 无法转换类型“绑定”的值<Data Struct?> &#39;到预期的参数类型&#39;绑定<Data Struct> &#39; - Cannot convert value of type 'Binding<Data Struct?>' to expected argument type 'Binding<Data Struct>'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM