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

孤人 提交于 2019-12-12 03:09:32

问题


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

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

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

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.


回答1:


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



回答2:


  WORequestManager.shared().genericRequest(withMethod: "GET", webserviceName: walletAPI, andParameters: params, showLoading: true, success: { (responseDictionary: [AnyHashable: Any]?) in


        }, failure: { (error: Error?) in

    })


来源:https://stackoverflow.com/questions/39537253/swift-2-to-swift-3-cannot-convert-value-of-type-data-nserror-void-to

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!