Swift 2 到 Swift 3:无法将类型 '(Data?, NSError?) -> Void' 的值转换为预期的参数类型 'GTMSessionFetcherCompletionHandler?'

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

我刚刚将工作中的 Swift 2 更新为 Swift 3 程序,但出现错误,

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

这里是相关的详细信息(我希望):

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

completionHandler 的函数:

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

GTMSessionFetcherCompletionHandler定义在一个Objective-C头中,如下:

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

我尝试将 handleDownload() 更改为以下内容:

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

但这会将错误移至此函数:"Cannot convert return expression of type '(Data?, NSError?) -> Void' to return type 'GTMSessionFetcherCompletionHandler?'"

我不知道如何保留柯里化 (?) 数据和错误变量,并让它编译。

根据 SE-0112, NSError is now bridged to Swift as the Error 协议。事实上,如果你 + 点击 GTMSessionFetcherCompletionHandler 输入 Swift,你会看到它是如何桥接的:

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

因此,您只需更改 handleDownload(_:) 的签名即可反映这一点:

func handleDownload(_ studentNum:Int) -> (Data?, Error?) -> Void {
    return { (data: Data?, error: Error?) -> Void in
        // code for function
    }
}
  WORequestManager.shared().genericRequest(withMethod: "GET", webserviceName: walletAPI, andParameters: params, showLoading: true, success: { (responseDictionary: [AnyHashable: Any]?) in


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

    })