Mike Strong Mike Strong - 8 months ago 39
Swift Question

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) {

override public init(frame:CGRect) {

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


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.