【问题标题】:Cannot invoke 'dataTask' with an argument list of type无法使用类型的参数列表调用“dataTask”
【发布时间】:2016-08-31 16:25:51
【问题描述】:

我有一段我几乎在我的代码中到处使用的普通代码:

let configuration = URLSessionConfiguration.default
let session = URLSession(configuration:configuration)
let listTask = session.dataTask(with: theRequest, completionHandler: {[weak self](data, response, error) in
})

然而在一个特定的类中,编译器抱怨:

不能使用类型为 '( 的参数列表: URLRequest, completionHandler: (Data?, URLResponse?, Error?) -> ())'

需要一个类型为 '(with: URLRequest, completionHandler: > (Data?, URLResponse?, Error?) -> Void)'的参数列表

它如何推断闭包的返回值是 () 而不是预期的 Void?我反复复制其他类的代码,以免写错了。

【问题讨论】:

  • 尝试删除[weak self]

标签: nsurlsession swift3


【解决方案1】:

由于某种原因添加:

return () 

在关闭结束时,按照 Apple 论坛上的建议,修复了该问题。

【讨论】:

    【解决方案2】:

    在 xcode 8 beta 6 和 swift 3 playground 中尝试过

        let configuration = URLSessionConfiguration.default
        let session = URLSession(configuration:configuration)
        let theRequest = URLRequest(url: URL(string: "")!)
        let task = session.dataTask(with: URL(string: "")!) { (data:Data?, response:URLResponse?, error:Error?) in
    
        }
    
        let task1 = session.dataTask(with: theRequest) { (data:Data?, response:URLResponse?, error:Error?) in
    
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-03-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多