Bird
0
0

How can you simplify this closure using Swift's shorthand argument names?

hard📝 Application Q9 of 15
iOS Swift - Swift Language Essentials
How can you simplify this closure using Swift's shorthand argument names?
let divide = { (a: Int, b: Int) -> Int in
  return a / b
}
Alet divide = { a, b -> Int in a / b }
Blet divide = { $0 / $1 }
Clet divide = { (x, y) in x / y }
Dlet divide = { (a: Int, b: Int) in return a / b }
Step-by-Step Solution
Solution:
  1. Step 1: Understand shorthand argument names

    Swift allows using $0, $1 for first and second closure parameters.
  2. Step 2: Identify the simplified closure

    let divide = { $0 / $1 } uses $0 and $1 to replace explicit parameters and return statement.
  3. Final Answer:

    let divide = { $0 / $1 } -> Option B
  4. Quick Check:

    Shorthand closure syntax = let divide = { $0 / $1 } [OK]
Quick Trick: Use $0, $1 for closure parameters to shorten code [OK]
Common Mistakes:
  • Omitting parameter types but keeping explicit names
  • Using return keyword unnecessarily
  • Confusing shorthand with named parameters

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More iOS Swift Quizzes