【问题标题】:Cannot assign value of type '() -> Void' to type '(() -> Void)!'无法将类型“()-> Void”的值分配给类型“(()-> Void)!”
【发布时间】:2016-12-24 10:27:30
【问题描述】:

更新到 xcode 8 beta 6 后,出现错误

无法将类型 '() -> Void' 的值分配给类型 '(() -> Void)!'

在下面的 func 块,第三行:

    // Button sub-class
public class SCLButton: UIButton {
    var actionType = SCLActionType.none
    var target:AnyObject!
    var selector:Selector!
    var action:(()->Void)!

    public init() {
        super.init(frame: CGRect.zero)
    }

    required public init?(coder aDecoder: NSCoder) {
        super.init(coder:aDecoder)
    }

    override public init(frame:CGRect) {
        super.init(frame:frame)
    }

//    required public init?(coder aDecoder: NSCoder) {
//        fatalError("init(coder:) has not been implemented")
//    }
}
public func addButton(_ title:String, action:()->Void)->SCLButton {
    let btn = addButton(title)
    btn.actionType = SCLActionType.closure
    btn.action = action // here is where the error occurs
    btn.addTarget(self, action:#selector(SCLAlertView.buttonTapped(_:)), for:.touchUpInside)
    btn.addTarget(self, action:#selector(SCLAlertView.buttonTapDown(_:)), for:[.touchDown, .touchDragEnter])
    btn.addTarget(self, action:#selector(SCLAlertView.buttonRelease(_:)), for:[.touchUpInside, .touchUpOutside, .touchCancel, .touchDragOutside] )
    return btn
}

对修复有什么建议吗?

【问题讨论】:

  • 错误消息只是说预期的类型是(() -> Void)!(隐式展开可选),但() -> Void(非可选)类型是在action参数中传递的。类型不匹配。
  • 检查更新的问题@ozgur
  • 那我应该调整什么? @vadian
  • 我想addButtonaction参数的签名(属性action不受影响)。
  • 检查更新的问题以获取更多详细信息@vadian

标签: ios swift xcode error-handling


【解决方案1】:

您的问题似乎与此有关: SE-0103

尝试将 addButton(_:action:) 的方法标头更改为:

public func addButton(_ title:String, action:@escaping ()->Void)->SCLButton {

新测试版的诊断信息像往常一样令人困惑和不足,但将您的属性设置为非可选var action:()->Void = {} 将为您提供更多有用的信息。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-27
    • 2017-09-21
    • 2018-03-03
    相关资源
    最近更新 更多