Home > Enterprise >  why does swift closure complain there is no init
why does swift closure complain there is no init

Time:04-14

I have a handler defined this way:

var handler: (String, (Bool) -> Void) -> Void

I am passing this in:

handler: ((String) -> Void)
                              { a in
                    print(a)
                }

I get this error and I don't understand how to fix it.

Type '(String) -> Void' has no member 'init'

When I tried this way I got the same error but I don't think it is correct as the handler passes in another closure with the boolean.

handler: (String, (Bool) -> Void) -> Void)
                              { a in
                    print(a)
                }

Error message and code screenshot

CodePudding user response:

The issue there is that the closure expects two parameters:

var handler: (String, (Bool) -> Void) -> Void = { _,_  in }

handler = { a, b in
    print(a)
    b(false)
}

handler("a") { bool in print(bool) }

This will print:

a
false

  • Related