将附加参数传递给没有咖喱函数的完成处理程序?
Pass additional parameters to completion handler without curried function?
我刚刚打开了一个我有一段时间没有修改过的项目并注意到一个警告:"Curried function declaration syntax will be removed in a future version of Swift; use a single parameter list"。
我不太确定在这种情况下如何抢先删除我的柯里化函数(对我来说这似乎是完美的解决方案)。我目前正在使用一个将附加参数传递给完成处理程序。
func getCoursesForProfile(profileName: String, pageNumber: Int) {
if let url = NSURL(string:profileBaseURL + profileName + pageBase + String(pageNumber)) {
let task = NSURLSession.sharedSession().dataTaskWithURL(url, completionHandler: parseSessionCompletion(profileName, pageNumber: pageNumber))
task.resume()
}
}
func parseSessionCompletion(profileName: String, pageNumber: Int)(data: NSData?, response: NSURLResponse?, error: NSError?) {
我的问题:有没有一种方法可以完成去除柯里化,同时仍然具有用于解析 "completed session" 的可重用函数?
我想到的唯一 "easy" 方法是拥有 class 的不同实例,并将 profileName/pageNumber 保留在函数范围之外。但这在很多方面似乎都是浪费。
柯里化并没有被移除——它只是定义被移除的柯里化函数的便利语法。现在您必须将柯里化函数定义为显式返回另一个函数(单个参数列表)。
例如,在您的情况下,您需要这样的东西:
func parseSessionCompletion(profileName: String, pageNumber: Int) -> (data: NSData?, response: NSURLResponse?, error: NSError?) -> () {
// do something
return {data, response, error in
// do something else
}
}
查看 proposal for the removal of the currying syntax 了解有关更改的更多信息。
我刚刚打开了一个我有一段时间没有修改过的项目并注意到一个警告:"Curried function declaration syntax will be removed in a future version of Swift; use a single parameter list"。
我不太确定在这种情况下如何抢先删除我的柯里化函数(对我来说这似乎是完美的解决方案)。我目前正在使用一个将附加参数传递给完成处理程序。
func getCoursesForProfile(profileName: String, pageNumber: Int) {
if let url = NSURL(string:profileBaseURL + profileName + pageBase + String(pageNumber)) {
let task = NSURLSession.sharedSession().dataTaskWithURL(url, completionHandler: parseSessionCompletion(profileName, pageNumber: pageNumber))
task.resume()
}
}
func parseSessionCompletion(profileName: String, pageNumber: Int)(data: NSData?, response: NSURLResponse?, error: NSError?) {
我的问题:有没有一种方法可以完成去除柯里化,同时仍然具有用于解析 "completed session" 的可重用函数?
我想到的唯一 "easy" 方法是拥有 class 的不同实例,并将 profileName/pageNumber 保留在函数范围之外。但这在很多方面似乎都是浪费。
柯里化并没有被移除——它只是定义被移除的柯里化函数的便利语法。现在您必须将柯里化函数定义为显式返回另一个函数(单个参数列表)。
例如,在您的情况下,您需要这样的东西:
func parseSessionCompletion(profileName: String, pageNumber: Int) -> (data: NSData?, response: NSURLResponse?, error: NSError?) -> () {
// do something
return {data, response, error in
// do something else
}
}
查看 proposal for the removal of the currying syntax 了解有关更改的更多信息。