I have been trying to learn the names of the Swift features recently so that I can communicate with people with these names.
For example
! is added to the end of a type name, it's called an "implicitly unwrapped optional type"guard denotes a "guard statement"if let x = someOptional is called "Optional binding"case 0...9: is an example of "pattern matching" in switch statementsBut there is one feature that I don't know the name of.
Usually, I write this kind of code because C-style for loops are deprecated:
for _ in 0...9 { print("hello") //print hello 10 times } I want to know what the underscore is called.
I know that this is from Ruby. So I think it will have the same name as the underscore in Ruby. So what exactly is the underscore called?
My guesses:
Is there an official term for this? If there isn't, how would I call this in a casual conversation about my code?
It's called a wildcard pattern:
Wildcard Pattern
A wildcard pattern matches and ignores any value and consists of an underscore (
_). Use a wildcard pattern when you don’t care about the values being matched against. For example, the following code iterates through the closed range1...3, ignoring the current value of the range on each iteration of the loop:for _ in 1...3 { // Do something three times. }
https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Patterns.html#//apple_ref/doc/uid/TP40014097-CH36-ID420
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With