What if you could write less code and still do the same job perfectly?
Why Shorthand argument names ($0, $1) in Swift? - Purpose & Use Cases
Imagine you want to sort a list of names or filter numbers using a function. You write a full function with named arguments every time, even for simple tasks.
This manual way is slow and clutters your code with extra words. It's easy to make mistakes by mixing up argument names or writing too much for simple actions.
Shorthand argument names like $0 and $1 let you write quick, clean code without naming each argument. They make your code shorter and easier to read for simple closures.
names.sorted(by: { name1, name2 in name1 < name2 })
names.sorted(by: { $0 < $1 })
You can write fast, clear code for small tasks without extra clutter, making your programs easier to understand and maintain.
When you want to quickly sort a list of scores or filter a list of words, shorthand arguments let you do it in one neat line instead of many.
Manual argument naming can be long and confusing.
Shorthand names $0, $1 simplify closures.
They make code shorter, cleaner, and easier to read.
Practice
$0 represent in a Swift closure?Solution
Step 1: Understand shorthand argument names
In Swift closures,$0refers to the first argument passed to the closure.Step 2: Identify
Since$0meaning$0is always the first argument, it represents the first input value the closure receives.Final Answer:
The first argument passed to the closure -> Option BQuick Check:
$0= first argument [OK]
- Confusing $0 with the return value
- Thinking $0 is the closure itself
- Mixing $0 with the last argument
Solution
Step 1: Understand sorting with closures
To sort ascending, the closure should return true if the first element is less than the second.Step 2: Match shorthand arguments to sorting logic
Using$0 < $1means the first element is less than the second, which sorts ascending.Final Answer:
let sorted = numbers.sorted { $0 < $1 } -> Option DQuick Check:
Ascending sort uses $0 < $1 [OK]
- Reversing $0 and $1 in comparison
- Using > instead of < for ascending
- Confusing sorting order logic
let names = ["Anna", "Bob", "Charlie"]
let reversed = names.map { String($0.reversed()) }
print(reversed)Solution
Step 1: Understand the map and reversed usage
Themapapplies the closure to each name.$0is each string.$0.reversed()reverses characters.Step 2: Convert reversed characters back to String
WrappingString($0.reversed())creates a reversed string for each name.Final Answer:
["annA", "boB", "eilrahC"] -> Option CQuick Check:
map + reversed string = reversed names [OK]
- Forgetting to convert reversed characters back to String
- Expecting original array without changes
- Confusing reversed string with spaced characters
let numbers = [3, 1, 2]
let sorted = numbers.sorted { $2 < $1 }
print(sorted)Solution
Step 1: Check shorthand argument usage
Shorthand arguments start at $0 for the first, $1 for the second. There is no $2 in a two-argument closure.Step 2: Identify invalid argument index
Using$2causes a compile error because only $0 and $1 exist here.Final Answer:
Using $2 which does not exist in this closure -> Option AQuick Check:
Only $0 and $1 allowed in two-argument closures [OK]
- Assuming $2 exists in two-argument closures
- Forgetting shorthand argument limits
- Confusing sorted and sort method differences
let pairs = [(3, 5), (1, 2), (4, 4)], which closure using shorthand argument names correctly sorts the array by the sum of each tuple's elements in descending order?Solution
Step 1: Understand tuple access and sorting logic
Each element is a tuple. To sort by sum descending, compare sums of $0 and $1 tuples.Step 2: Check each option's correctness
pairs.sorted { $0.0 + $0.1 > $1.0 + $1.1 } sums $0.0 + $0.1 and compares to $1.0 + $1.1 with > for descending order, which is correct.Final Answer:
pairs.sorted { $0.0 + $0.1 > $1.0 + $1.1 } -> Option AQuick Check:
Sum tuples and use > for descending sort [OK]
- Mixing $0 and $1 order in comparison
- Trying to add tuples directly without accessing elements
- Using incorrect tuple element indices
Ready to prove it?
Pass the test and this pattern moves unknown
