Reputation: 58097
I used the map
function in Swift to iterate on a bunch of subviews and remove them from a superview.
self.buttons.map { $0.removeFromSuperview() }
When I upgraded from Swift 1.x to 2.0, Xcode gave a warning that the return value from map was unused. So I assigned it with let x = ...
and I got another warning:
So I let Xcode fix the warning for me, and it gave me this:
_ = self.buttons.map { $0.removeFromSuperview() }
What is the significance of an underscore when not in the context of a method parameter? What does it mean?
I do know that when a method parameter is anonymous, the underscore takes their place. I'm talking about an underscore in the middle of a method. It's not part of a message.
Upvotes: 9
Views: 1504
Reputation: 82
If I'm not mistaken this carried over from Objective-C. When you did not "use" your pointer at all in the app, you would get a warning. In Swift 2 I believe they tried to implement something similar.
Upvotes: -1
Reputation: 162
A parameter whose local name is an underscore is ignored. The caller must supply an argument, but it has no name within the function body and cannot be referred to there. It is basically a parameter placeholder that you are not going to use for anything in the body.
Upvotes: 3
Reputation: 2666
An underscore denotes the fact that you need to set a variable, but you do not plan on using the variable in the future. Instead of giving it an elaborate name, an underscore will be more simple and less clutter, especially in the case that the variable name doesn't matter. (since you do not use it again.)
Upvotes: 9