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 '(() -> ())?

+3


source to share


4 answers


Because it clickAction

expects a function / parameter closure. Just change your code to:



button.clickAction = {
    action()
    Sound.playSound(Sounds.Button)
}

      

+4


source


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.

+3


source


It seems to me that your edit has its own answer: the type of closure is () -> (), but you provide your parameter closure.

0


source


I had a similar problem, I solved it like this:

button.clickAction = { _ in
        action()
        Sound.playSound(Sounds.Button)
    }

      

Hope this helps you.

0


source







All Articles