Sunday, November 6, 2016

Underscore Uses in Swift

Dmitri Pavlutin (via Natasha Murashev):

You can use _ to suppress the argument label every time its indication seems redundant.

[…]

let (_, sub, _, div) contains _ at first and third positions. This indicates that addition and multiplication results are insignificant.

[…]

The flight speed case .flight(let speed, _) omits the information about the altitude using an underscore _. Extracting the altitude in this case is simply not necessary.

[…]

Otherwise you can discard the result using an underscore […] Such way you let know the compiler that you ignore the returned value. And no warning is triggered in this case.

[…]

If you need to skip naming some closure parameters, just mark those with underscores.

[…]

Another common usage of the underscore is to iterate a code block a particular number of times, and ignore the iteration value.

Comments RSS · Twitter

Leave a Comment