【问题标题】:Cannot convert value of type '() -> Void' to expected argument type '(() -> Void)?'无法将类型“() -> Void”的值转换为预期的参数类型“(() -> Void)?”
【发布时间】:2017-01-13 23:06:51
【问题描述】:

我在尝试转换为 Swift 3 时尝试修复我的结局错误,但是我不确定为什么会收到错误

无法将 '() -> Void' 类型的值转换为预期的参数类型 '(() -> 无效)?'

对于

 CATransaction.setCompletionBlock(completion)

功能齐全

fileprivate func deleteRowsAtIndexPaths(_ indexPaths: [IndexPath], withRowAnimation animation: UITableViewRowAnimation, duration: TimeInterval, completion:() -> Void) {
    CATransaction.begin()
    CATransaction.setCompletionBlock(completion) //Error
    UIView.animate(withDuration: duration) { () -> Void in
        self.deleteRows(at: indexPaths, with: animation)
    }
    CATransaction.commit()
}

有谁知道我为什么会得到这个以及如何解决这个错误?

【问题讨论】:

标签: ios swift swift3


【解决方案1】:

如果您在将代码迁移到 Swift 3 时发现异常情况,请查看 the latest reference

声明

class func setCompletionBlock(_ block: (@escaping () -> Void)?)

尝试将方法头 completion:() -> Void 的这一部分更改为:

completion: @escaping () -> Void

【讨论】:

  • 有必要写@ escaping 吗?
  • @technerd,是的。它与this 有关。您需要传播@escaping 的要求,以帮助编译器检测非转义(现在它已成为默认)闭包的非法使用。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-03-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-19
  • 2022-01-21
相关资源
最近更新 更多