Chris Gregg Chris Gregg - 1 year ago 123
Swift Question

Swift 2 to Swift 3: Cannot convert value of type '(Data?, NSError?) -> Void' to to expected argument type 'GTMSessionFetcherCompletionHandler?'

I just updated a working Swift 2 to Swift 3 program, and I am getting the error,

Cannot convert value of type '(Data?, NSError?) -> Void' to expected argument type 'GTMSessionFetcherCompletionHandler?'

Here are the relevant details (I hope):

let fetcher = GTMSessionFetcher(urlString:url)
fetcher.authorizer = parentController.service.authorizer
fetcher.beginFetch(completionHandler: handleDownload(studentNum))
^^^^ causing the error

The function for the completionHandler:

func handleDownload(_ studentNum:Int) -> (Data?, NSError?) -> Void {
return { (data: Data?, error: NSError?) -> Void in
// code for function

is defined in an Objective-C header, as follows:

#define GTM_NULLABLE_TYPE __nullable
typedef void (^GTMSessionFetcherCompletionHandler)(NSData * GTM_NULLABLE_TYPE data,

I have tried changing handleDownload() to the following:

func handleDownload(_ studentNum:Int) -> (GTMSessionFetcherCompletionHandler?) {
return { (data: Data?, error: NSError?) -> Void in
// code for function

but that moves the error down to this function: "Cannot convert return expression of type '(Data?, NSError?) -> Void' to return type 'GTMSessionFetcherCompletionHandler?'"

I can't figure out how to keep the curried (?) data and error variables, and have it compile.

Answer Source

As per SE-0112, NSError is now bridged to Swift as the Error protocol. In fact, if you + click on the GTMSessionFetcherCompletionHandler type in Swift, you'll see exactly how it's bridged:

typealias GTMSessionFetcherCompletionHandler = (Data?, Error?) -> Void

Therefore you simply need to change your handleDownload(_:)'s signature to reflect this:

func handleDownload(_ studentNum:Int) -> (Data?, Error?) -> Void {
    return { (data: Data?, error: Error?) -> Void in
        // code for function
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download