gpt4 book ai didi

swift - 无法将 Swift 2.2 currying 转换为 future 的 Swift 格式

转载 作者:可可西里 更新时间:2023-11-01 02:16:38 24 4
gpt4 key购买 nike

我有以下 curried 函数,我收到 Xcode 警告,Curried 函数语法将在 Swift 的 future 版本中被删除;使用单个参数列表,但建议的修复不起作用(它只是将参数组合到一个函数调用中)。我正在尝试转换为新格式,但我不明白它是如何工作的。下面的 beginFetchWithCompletionHandler 函数期望 handleDownload 具有 (data : NSData?, error : NSError?) 的参数签名。

fetcher.beginFetchWithCompletionHandler(handleDownload)

我还想传入一个整数,如下:

fetcher.beginFetchWithCompletionHandler(handleDownload(0))

以下柯里化(Currying)函数完美运行(但给出警告):

func handleDownload(iCount : Int)(data : NSData?, error : NSError?) -> Void {
print(iCount)
print(data!.length)
}

这是我尝试过的方法,但出现错误“表达式解析为未使用的函数”:

func handleDownload2(iCount:Int) -> (NSData?, NSError?) -> Void {
return { (data: NSData?, error: NSError?) -> Void in {
// received image
print(iCount)
print(data!.length)
}
}
}

最佳答案

里面的大括号写错了,应该是:

func handleDownload2(iCount:Int) -> (NSData?, NSError?) -> Void {
return { (data: NSData?, error: NSError?) -> Void in
// received image
print(iCount)
print(data!.length)
}
}

关于swift - 无法将 Swift 2.2 currying 转换为 future 的 Swift 格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37739173/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com