【发布时间】:2018-03-16 16:45:26
【问题描述】:
我一直在将我的项目代码迁移到 Swift 4.0。
我遇到了关闭问题。在下面的代码sn-p中:
class ViewController: UIViewController
{
var completionClosure: ((Void)->(Void))? //WARNING: When calling this function in Swift 4 or later, you must pass a '()' tuple; did you mean for the input type to be '()'?
override func viewDidLoad()
{
super.viewDidLoad()
self.completionClosure = { //ERROR: Cannot assign value of type '() -> ()' to type '((Void) -> (Void))?'
print("This is a closure.")
}
}
}
上述代码在 Swift 3.2 中完美运行。在 Swift 4.0 中,它给了我这些警告和错误。
我知道如果闭包不包含任何input arguments and return type,则使用Void 是没有意义的,即闭包可以这样写:
var completionClosure: (()->())?
但是,为什么它给了我错误? Void和No Value不一样吗?
自 Swift 4.0 以来,闭包的概念有什么变化吗?
【问题讨论】:
-
@OOPer 的这个 SO 回答可以帮助你。 stackoverflow.com/a/45184498/488506