I have encountered _: in several places without explanation. What does this mean/do?
Basically it means “anonymous parameter”. If I define a function like
func doSomethingWith(item: Item)
then I can only call it like
But if I define it as
func doSomethingWithItem(_ item: Item)
then the right way to use it is
In the second case there’s no need to name the parameter you pass to the function. Sometimes it doesn’t make sense to be always labelling the first thing you pass, such as when in a protocol, the first argument is always the caller, such as a view controller calling its delegate.