Recall & Review
beginner
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?✗ 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?
✗ 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?✗ 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?
✗ 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?
✗ 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.