Swift 4: Cannot assign value like '(_) & # 8594; Void 'for input' (() -> ())? '
XCode 9 Beta 3. Swift 4.
let button = JumpingButton(x: 0, y: 50, w: 150, h: 300) // JumpingButton: UIButton
//Inside JumpingButton: // var clickAction: (() -> ())?
button.clickAction = { (sender) -> Void in //Error line
action()
Sound.playSound(Sounds.Button)
}
Getting error: Unable to assign a value of type '(_) -> Void' to input '(() -> ())?
source to share
I don't know anything about the API of these functions (you never told us what it is), but here's what the error says:
Cannot assign a value of type
This refers to passing parameters, which is a kind of "assignment"
'(_) → Void'
This is the type of argument you gave to the parameter. It has some parameter of unknown type (_)
and returns ( ->
) Void
.
enter '(() → ())?'
This is the type of argument that was expected for this parameter. It has no parameters ( ()
), it returns ( ->
) Void
( ()
) and optionally ( (...)?
)
So the problem is when you are passing a closure with a parameter as an argument to a parameter that expects a closure with no parameters.
source to share