将其他参数传递给完成处理程序而不使用curried函数?

时间:2016-05-03 21:45:31

标签: swift currying

我刚刚打开了一个我没有修改过的项目,并注意到一个警告:“将在未来版本的Swift中删除Curried函数声明语法;使用单个参数列表”。

我不太确定如何在这种情况下抢先删除我的curried功能(这对我来说似乎是完美的解决方案)。我目前正在使用一个将其他参数传递给完成处理程序。

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?) {

我的问题:有没有办法完成去除currying,同时仍然具有解析“完成的会话”的可重用功能?

我想要解决的唯一“简单”方法是拥有一个类的不同实例,并将profileName / pageNumber保持在函数范围之外......但这在多种方面看起来很浪费。

1 个答案:

答案 0 :(得分:1)

不会删除Currying - 它只是用于定义被删除的curry函数的便捷语法。现在,您必须将curried函数定义为显式返回另一个函数(单个参数列表)。

例如,在您的情况下,您会想要这样的内容:

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以获取有关更改的更多信息。

相关问题