Bird
0
0

Find the error in this Swift code:

medium📝 Debug Q7 of 15
iOS Swift - Swift Language Essentials
Find the error in this Swift code:
let numbers = [1, 2, 3]
let doubled = numbers.map { num in
  num * 2
}
Amap cannot be used with closures
BNo error, code is correct
CClosure parameter missing type
DMissing return keyword inside closure
Step-by-Step Solution
Solution:
  1. Step 1: Review map and closure usage

    map accepts a closure; 'num in num * 2' is valid shorthand syntax.
  2. Step 2: Confirm no syntax errors

    Return keyword is optional for single-expression closures.
  3. Final Answer:

    No error, code is correct -> Option B
  4. Quick Check:

    map with closure syntax = correct [OK]
Quick Trick: Single-expression closures omit 'return' [OK]
Common Mistakes:
  • Thinking 'return' is always required
  • Believing map can't use closures
  • Expecting parameter types are mandatory

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More iOS Swift Quizzes