【问题标题】:Using completionhandler from function variable in NSURLConnection's sendAsynchronousRequest issue在 NSURLConnection 的 sendAsynchronousRequest 问题中使用函数变量中的完成处理程序
【发布时间】:2015-07-24 19:38:08
【问题描述】:

我有一个功能:

func connectHTTP(requests: String, completion:(NSURLResponse!, NSData!, NSError!) -> Void) {

    (...)

    NSURLConnection.sendAsynchronousRequest(httpRequest, queue: queue, completionHandler: completion)

}

如果我像这样打电话给connectHTTP

connectHTTP(requests, completion: {
            (response: NSURLResponse!, data: NSData!, error: NSError!) -> Void in
            NSLog(response)
})

编译器输出错误:

不能使用类型为 '(String, completion: (NSURLResponse!, NSData!, NSError!) 的参数列表调用 'connectHTTP' -> 无效)'

有人知道为什么吗?


似乎错误消息只是错误的,并指向闭包内的错误。

【问题讨论】:

  • 您的示例代码有问题。密码:应该在那里吗?
  • 对不起,是的,原来的代码稍微复杂一些,我在简化时出错了。
  • 请注意,如果我删除NSLog(response),错误就会消失

标签: swift cocoa nsurlconnection completionhandler


【解决方案1】:

我认为正确的通过闭包的方式是这样的:

connectHTTP(requests) {
    (response, data, error) in
    NSLog(response)
}

【讨论】:

  • 谢谢你的回答,现在的错误是 Cannot invoke 'connectHTTP' with an argument list of type '(String, completion: (_, _, _) -> _)'
猜你喜欢
  • 2012-10-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多