Difference between returning Void and () in a swift closure

前端 未结 4 1432
灰色年华
灰色年华 2020-12-25 12:53

Whats the difference between these closures?

let closureA: () -> ()

and

let closureB: () -> Void
4条回答
  •  失恋的感觉
    2020-12-25 13:22

    If you arrived here because you got an error such as Cannot convert value of type 'Void.Type' to specified type 'Void', then it might be useful to consider that:

    () can mean two things:

    1. () can be a type - the empty tuple type, which is the same as Void.
    2. () can be a value - an empty tuple, which is the same as Void().

    In my case I was looking to change () to Void, but this caused the error mentioned above. Solving it would either mean just keeping it as () or using Void(). The latter can be put in a global constant let void = Void() and then you can just use void. Whether this is a good practice / style, is up for debate.

提交回复
热议问题