Mythics Winter Mythics Winter - 1 year ago 80
Swift Question

Pass additional parameters to completion handler without curried function?

I just opened a project I haven't tinkered with in a little while and noticed a warning: "Curried function declaration syntax will be removed in a future version of Swift; use a single parameter list".

I'm not quite sure how to preemptively remove my curried function in this case (it seems like the perfect solution to me). I'm currently using one to pass additional parameters to a completion handler.

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


func parseSessionCompletion(profileName: String, pageNumber: Int)(data: NSData?, response: NSURLResponse?, error: NSError?) {

My question: Is there a way to accomplish the removal of currying while still having a reusable function for parsing the "completed session"?

The only "easy" way I'm thinking of to go about this would be to have different instances of a class and keep profileName/pageNumber outside the scope of the functions.. but that seems wasteful in multiple ways.

Answer Source

Currying isn't being removed – it's just the convenience syntax for defining a curried function that's been removed. Now you have to define a curried function as explicitly returning another function (a single parameter list).

For example in your case, you'll want something like this:

func parseSessionCompletion(profileName: String, pageNumber: Int) -> (data: NSData?, response: NSURLResponse?, error: NSError?) -> () {

    // do something

    return {data, response, error in
        // do something else

Take a look at the proposal for the removal of the currying syntax for more info about the change.