小编典典

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

swift

更新到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
}

关于修复的任何建议?


阅读 3260

收藏
2020-07-07

共1个答案

小编典典

看来您的问题与此有关: SE-0103

尝试将您的方法标头追踪addButton(_:action:)到:

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

与往常一样,来自新Beta的诊断消息是如此混乱和不足,但是将您的媒体资源设置为非可选var action:()->Void = {}将为您提供更多有用的信息。

2020-07-07