Swift
Optional or no-op callbacks?

by Cristián Arenas Ulloa

var callback: ((Int)->Void)? = nil

callback?(1)

var callback: (Int)->Void = { _ in }

callback(1)

If your callback returns a value different from Void you can do more with the optional version:


callbackReturnsInt?(42) ?? 100

Most of the time it will be hard to find a good default value for a no-op callback.

Adrian Z.

Sign in to comment!