Use of Anonymous Label AND Parameter Name

useful examples are very welcome.

Here’s an example I hit regularly:

func didTimeOut(_: Timer) {
    … code …
}

Timer.scheduledTimer(withTimeInterval: 1.0, repeats: false, block: didTimeOut)

The timer passes itself to the callback because, in the case of a repeating timer, it’s useful to have the timer handy so that you can invalidate it. However, in the case of a one-shot timer that’s unnecessary and the _ makes it clear that I planned to ignore it.

Share and Enjoy

Quinn “The Eskimo!” @ DTS @ Apple

1 Like