Bird
0
0

Examine this Swift function:

medium📝 Debug Q6 of 15
Swift - Optionals
Examine this Swift function:
func welcome(_ user: String?) {
    guard let name = user
    print("Welcome, \(name)!")
}

What is the issue with this code?
AThe optional <code>user</code> should not be unwrapped with <code>guard let</code>.
BMissing <code>else</code> block after <code>guard let</code> statement.
CThe print statement should be inside the <code>else</code> block.
DThe function should return a value instead of printing.
Step-by-Step Solution
Solution:
  1. Step 1: Check guard let syntax

    A guard let statement must always have an else block to handle the nil case.
  2. Step 2: Identify missing else

    The code lacks the required else block after guard let name = user.
  3. Final Answer:

    Missing else block after guard let statement. -> Option B
  4. Quick Check:

    Guard requires else block [OK]
Quick Trick: Guard let always needs an else block [OK]
Common Mistakes:
  • Omitting the else block after guard let
  • Placing code that should be in else outside
  • Misunderstanding guard let syntax requirements

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Swift Quizzes