swift 4 : Cannot convert value of type '(_) -> ()' to expected argument type '() -> ()' or Argument passed to call that takes no arguments

标签 swift closures swift4

使用 XCode 9,Beta 3。Swift 4。

statsView.createButton("Button name") { [weak self] Void in
   //stuff stuff
   self?.doSomething()
}

我遇到了数百个这样的错误,我该如何修复它们?

错误:

Cannot convert value of type '(_) -> ()' to expected argument type '() -> ()'

Argument passed to call that takes no arguments

最佳答案

我们似乎不再在 Swift 4 中使用 Void in 了。我是如何修复它们的:

删除 Void 关键字:

statsView.createButton("Button name") { [weak self] in
   //stuff stuff
   self?.doSomething()
}

你必须使用 in 或编译器 complain with

Expected ',' separator

如果没有参数,则根本不要使用 Void in

statsView.createButton("Button name") { //No "Void in" in here
   print("hi")
}

关于 swift 4 : Cannot convert value of type '(_) -> ()' to expected argument type '() -> ()' or Argument passed to call that takes no arguments,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45163720/

相关文章:

javascript - 如何预防和防范闭包内存泄漏

ios - 键盘在 Swift 4 中与 UITextField 重叠

swift - 错误: cannot convert value of type 'String' to expected argument type 'Character'

swift - 在惰性构造函数中访问实例常量

c++ - 这些函数为什么不衰减到函数指针?

ios - AVAssetExportSession 只工作前 6 次

javascript - 用 every 和 $.post 关闭

ios - UITextField 自定义清除按钮

swift - 喜欢大标题和 RefreshControl 效果不佳

ios - 无法对齐数组中的图像(图集)-SpriteKit