Chris Gregg Chris Gregg - 5 months ago 35
Swift Question

Cannot translate Swift 2.2 currying to future Swift format

I have the following curried function and I received an XCode warning that Curried function syntax will be removed in a future version of Swift; use a single parameter list, but the suggested fix-it didn't work (it simply combined the parameters into one function call). I am trying to convert to the new format, but I don't understand how it works. The

beginFetchWithCompletionHandler
function below expects
handleDownload
to have the parameter signature of
(data : NSData?, error : NSError?)
.

fetcher.beginFetchWithCompletionHandler(handleDownload)


I want to also pass in an integer, as follows:

fetcher.beginFetchWithCompletionHandler(handleDownload(0))


The following curried function works perfectly (but gives the warning):

func handleDownload(iCount : Int)(data : NSData?, error : NSError?) -> Void {
print(iCount)
print(data!.length)
}


Here is what I've tried, but I get the error, "Expression resolves to an unused function":

func handleDownload2(iCount:Int) -> (NSData?, NSError?) -> Void {
return { (data: NSData?, error: NSError?) -> Void in {
// received image
print(iCount)
print(data!.length)
}
}
}

Answer

The inner curly braces are wrong, it should be:

func handleDownload2(iCount:Int) -> (NSData?, NSError?) -> Void {
    return { (data: NSData?, error: NSError?) -> Void in
        // received image
        print(iCount)
        print(data!.length)
    }
}
Comments