问题
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