Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
293 views
in Technique[技术] by (71.8m points)

ios - Cannot assign value of type '() -> Void' to type '(() -> Void)!'

after updating to xcode 8 beta 6, I'm getting the error

Cannot assign value of type '() -> Void' to type '(() -> Void)!'

on the following func block, third line:

    // 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
}

any suggestions on a fix?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

It seems your issue is related to this: SE-0103

Try chaging the method header of your addButton(_:action:) to:

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

The diagnostic messages from new betas are so confusing and inadequate as usual, but making your property simply non-Optional var action:()->Void = {} will give you a little more useful info.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...