Arguments Have a Shorthand, Too
As it turns out, you don’t even have to declare s1
and s2
as the parameters. You can use a special syntax to get the nth parameter. You can write $0 to get the first parameter and $1 to get the second parameter and so on. Now the closure gets even shorter because you no longer need to declare the names of parameters. You can pass the closure to sort
like so:
sort(&names,{$0 < $1})
You are able to write this because of all the things mentioned before plus the fact that you don’t have to declare parameters if you use a special argument shorthand. Why declare something that does not need to be declared? You don’t need the return
keyword, and you don’t need to declare a type for the parameters because it is inferred. ...
Get Learning Swift™ Programming now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.