【发布时间】:2014-10-29 14:51:10
【问题描述】:
所以我定义了以下函数:
public typealias RESTClosure = (response: NSURLResponse!, data: NSData!, error: NSError!) -> Void
public func queryAllFlightsWithClosure(completionHandler : RESTClosure) {
// code ....
}
我可以将此函数称为:
func myResponseHandler(response: NSURLResponse!, data: NSData!, error: NSError!) -> Void {
// code ...
}
rest?.queryAllFlightsWithClosure(myResponseHandler)
然而,根据我对 Swift 的理解,如果函数的最后一个参数是闭包,它可以转换为尾随闭包……但我遇到了一些语法混乱:
尝试 #1
rest?.queryAllFlightsWithClosure() {
println("Called with Closure")
}
错误: 元组类型 '(response: NSURLResponse!, data NSData!, error: NSError!)' 和 '()' 有不同数量的元素(3 vs. 0)
尝试 #2
rest?.queryAllFlightsWithClosure() (RESTClosure.self) { // Xcode told me to add a .self
//...code
}
错误: 调用中的参数 #1 缺少参数
我知道我已经很接近了......但是有人可以在这里帮助我吗?
【问题讨论】: