Swift 2到Swift 3:无法转换类型'(Data ?, NSError?)->的值无效"到期望的参数类型"GTMSessionFetcherCompletionHandler?" [英] Swift 2 to Swift 3: Cannot convert value of type '(Data?, NSError?) -> Void' to to expected argument type 'GTMSessionFetcherCompletionHandler?'
问题描述
我刚刚将工作正常的Swift 2更新为Swift 3程序,但出现了错误,
I just updated a working Swift 2 to Swift 3 program, and I am getting the error,
无法将类型'(Data ?, NSError?)-> Void'的值转换为预期的参数类型'GTMSessionFetcherCompletionHandler?'
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
completionHandler的函数:
The function for the completionHandler:
func handleDownload(_ studentNum:Int) -> (Data?, NSError?) -> Void {
return { (data: Data?, error: NSError?) -> Void in
// code for function
}
}
GTMSessionFetcherCompletionHandler
在Objective-C标头中定义,如下所示:
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);
我尝试将handleDownload()更改为以下内容:
I have tried changing handleDownload() to the following:
func handleDownload(_ studentNum:Int) -> (GTMSessionFetcherCompletionHandler?) {
return { (data: Data?, error: NSError?) -> Void in
// code for function
}
}
但是将错误移至该函数:无法将类型'(Data ?, NSError?)-> Void'的返回表达式转换为'GTMSessionFetcherCompletionHandler吗?'"
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.
推荐答案
根据 Error
桥接到Swift.协议.实际上,如果您⌥ +单击Swift中的GTMSessionFetcherCompletionHandler
类型,您将确切看到它是如何桥接的:
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
因此,您只需要更改handleDownload(_:)
的签名即可反映这一点:
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
}
}
这篇关于Swift 2到Swift 3:无法转换类型'(Data ?, NSError?)->的值无效"到期望的参数类型"GTMSessionFetcherCompletionHandler?"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!