Bird
0
0

Identify the error in this Swift code using guard:

medium📝 Debug Q6 of 15
Swift - Control Flow
Identify the error in this Swift code using guard:
func validate(_ input: String?) {
    guard input != nil else
        print("Input is nil")
        return
    print("Input is \(input!)")
}
AGuard condition should be inside parentheses
BUsing force unwrap instead of optional binding
CMissing braces {} after else
DNo error, code is correct
Step-by-Step Solution
Solution:
  1. Step 1: Check guard syntax requirements

    A guard statement must have an else block enclosed in braces { }.
  2. Step 2: Identify missing braces

    The code lacks braces after else, causing a syntax error.
  3. Final Answer:

    Missing braces {} after else -> Option C
  4. Quick Check:

    Guard else block always needs braces [OK]
Quick Trick: Always use braces {} after guard else [OK]
Common Mistakes:
  • Omitting braces after else
  • Forgetting to return or exit in else block
  • Using force unwrap instead of safe binding

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Swift Quizzes