What are shorthand argument names like $0 and $1 in Swift?
They are automatic names for closure parameters, used to refer to the first, second, etc., arguments without explicitly naming them.
Click to reveal answer
beginner
How do you use $0 in a Swift closure?
Inside a closure, $0 refers to the first argument passed to the closure, allowing you to write shorter code without naming parameters.
Click to reveal answer
beginner
Example: What does this Swift code print?<br><pre>let numbers = [1, 2, 3]
let doubled = numbers.map { $0 * 2 }
print(doubled)</pre>
It prints [2, 4, 6]. The map function uses $0 to refer to each number in the array and doubles it.
Click to reveal answer
intermediate
Can you use $1 in a Swift closure? When?
Yes, $1 refers to the second argument passed to the closure. Use it when the closure takes two or more parameters.
Click to reveal answer
beginner
Why might shorthand argument names be helpful?
They make code shorter and cleaner by removing the need to write explicit parameter names, especially in simple closures.
Click to reveal answer
In Swift, what does $0 represent inside a closure?
AThe closure itself
BThe closure's return value
CThe first argument passed to the closure
DThe last argument passed to the closure
✗ Incorrect
$0 is the shorthand name for the first argument passed to a closure.
Which of these is a valid use of shorthand argument names in Swift?
Aarray.map { $0 + 1 }
Barray.map { first + 1 }
Carray.map { $2 + 1 }
Darray.map { $ }
✗ Incorrect
$0 is valid for the first argument; $2 is invalid if closure has fewer arguments.
When should you use $1 in a Swift closure?
ANever, it's invalid
BOnly when the closure has one parameter
CWhen you want the closure's return value
DWhen the closure has at least two parameters
✗ Incorrect
$1 refers to the second parameter, so use it when the closure has two or more parameters.
What is the benefit of using shorthand argument names in Swift closures?
AMakes code shorter and easier to read for simple closures
BImproves program speed
CAllows naming parameters explicitly
DPrevents errors in closures
✗ Incorrect
Shorthand argument names reduce code clutter and improve readability in simple closures.
Which of the following is NOT a valid shorthand argument name in Swift closures?
A$0
B$10
C$2
D$1
✗ Incorrect
Swift supports shorthand argument names up to the number of parameters passed; $10 is invalid if closure has fewer than 11 parameters.
Explain how shorthand argument names like $0 and $1 work in Swift closures.
Think about how closures can be shorter without naming parameters.
You got /3 concepts.
Give an example of using $0 in a Swift closure and describe what it does.
Use a simple array operation like map.
You got /3 concepts.
Practice
(1/5)
1. What does the shorthand argument $0 represent in a Swift closure?
easy
A. The last argument passed to the closure
B. The first argument passed to the closure
C. The closure itself
D. The return value of the closure
Solution
Step 1: Understand shorthand argument names
In Swift closures, $0 refers to the first argument passed to the closure.
Step 2: Identify $0 meaning
Since $0 is always the first argument, it represents the first input value the closure receives.
Final Answer:
The first argument passed to the closure -> Option B
Quick Check:
$0 = first argument [OK]
Hint: Remember $0 is always the first input in closures [OK]
Common Mistakes:
Confusing $0 with the return value
Thinking $0 is the closure itself
Mixing $0 with the last argument
2. Which of the following is the correct way to use shorthand argument names to sort an array of integers in ascending order?
easy
A. let sorted = numbers.sorted { $1 > $0 }
B. let sorted = numbers.sorted { $1 < $0 }
C. let sorted = numbers.sorted { $0 > $1 }
D. let sorted = numbers.sorted { $0 < $1 }
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 < $1 means the first element is less than the second, which sorts ascending.
Final Answer:
let sorted = numbers.sorted { $0 < $1 } -> Option D
Quick Check:
Ascending sort uses $0 < $1 [OK]
Hint: Use $0 < $1 for ascending sort closures [OK]
Common Mistakes:
Reversing $0 and $1 in comparison
Using > instead of < for ascending
Confusing sorting order logic
3. What is the output of the following Swift code?
let names = ["Anna", "Bob", "Charlie"]
let reversed = names.map { String($0.reversed()) }
print(reversed)
medium
A. ["annA", "boB", "eilrahc"]
B. ["Anna", "Bob", "Charlie"]
C. ["annA", "boB", "eilrahC"]
D. ["a n n A", "b o B", "c h a r l i e"]
Solution
Step 1: Understand the map and reversed usage
The map applies the closure to each name. $0 is each string. $0.reversed() reverses characters.
Step 2: Convert reversed characters back to String
Wrapping String($0.reversed()) creates a reversed string for each name.
Final Answer:
["annA", "boB", "eilrahC"] -> Option C
Quick Check:
map + reversed string = reversed names [OK]
Hint: Remember map applies closure to each element using $0 [OK]
Common Mistakes:
Forgetting to convert reversed characters back to String
Expecting original array without changes
Confusing reversed string with spaced characters
4. Identify the error in this Swift code using shorthand argument names:
let numbers = [3, 1, 2]
let sorted = numbers.sorted { $2 < $1 }
print(sorted)
medium
A. Using $2 which does not exist in this closure
B. Missing return keyword in closure
C. Incorrect array declaration syntax
D. Using sorted instead of sort method
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 $2 causes a compile error because only $0 and $1 exist here.
Final Answer:
Using $2 which does not exist in this closure -> Option A
Quick Check:
Only $0 and $1 allowed in two-argument closures [OK]
Hint: Use only $0 and $1 for two-parameter closures [OK]
Common Mistakes:
Assuming $2 exists in two-argument closures
Forgetting shorthand argument limits
Confusing sorted and sort method differences
5. Given the array 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?
hard
A. pairs.sorted { $0.0 + $0.1 > $1.0 + $1.1 }
B. pairs.sorted { $1.0 + $1.1 > $0.0 + $0.1 }
C. pairs.sorted { $0 + $1 > $1 + $0 }
D. pairs.sorted { $0.0 > $1.1 }
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.