Challenge - 5 Problems
Swift Guard Let Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of this Swift code using guard let?
Consider the following Swift function that uses
guard let for early exit. What will be printed when calling checkName(nil)?Swift
func checkName(_ name: String?) { guard let unwrappedName = name else { print("Name is missing") return } print("Hello, \(unwrappedName)!") } checkName(nil)
Attempts:
2 left
💡 Hint
Think about what happens when the optional is nil and how guard let handles it.
✗ Incorrect
The guard statement checks if 'name' is non-nil. Since 'name' is nil, the else block runs, printing 'Name is missing' and returning early.
❓ Predict Output
intermediate2:00remaining
What does this Swift function print when given a non-nil value?
Analyze this Swift function using
guard let. What will be printed when calling checkAge(25)?Swift
func checkAge(_ age: Int?) { guard let validAge = age, validAge >= 18 else { print("Underage or no age provided") return } print("Access granted for age \(validAge)") } checkAge(25)
Attempts:
2 left
💡 Hint
Check the conditions inside the guard statement carefully.
✗ Incorrect
The guard unwraps 'age' and checks if it is 18 or older. Since 25 meets both conditions, the else block is skipped and the success message prints.
🔧 Debug
advanced2:00remaining
Identify the error in this Swift code using guard let
This Swift function intends to unwrap an optional string and print it. What error will occur when compiling or running this code?
Swift
func printMessage(_ message: String?) { guard let message = message else { print("No message to print") return } print(message) } printMessage(nil)
Attempts:
2 left
💡 Hint
Remember what guard else block must always do.
✗ Incorrect
The guard else block must exit the current scope (return, break, continue, or throw). Here it only prints but does not return, causing a compilation error.
❓ Predict Output
advanced2:00remaining
What is the output of this nested guard let example?
Consider this Swift function with nested
guard let statements. What will be printed when calling processData(name: "Alice", age: nil)?Swift
func processData(name: String?, age: Int?) { guard let validName = name else { print("Name missing") return } guard let validAge = age else { print("Age missing") return } print("Name: \(validName), Age: \(validAge)") } processData(name: "Alice", age: nil)
Attempts:
2 left
💡 Hint
Check which guard fails first and what it prints.
✗ Incorrect
The first guard unwraps 'name' successfully. The second guard fails because 'age' is nil, so it prints 'Age missing' and returns early.
🧠 Conceptual
expert2:00remaining
Why is guard let preferred for early exit in Swift?
Which of the following best explains why Swift developers use
guard let for early exit instead of nested if-let statements?Attempts:
2 left
💡 Hint
Think about code readability and flow control.
✗ Incorrect
Guard let requires an else block that exits early, which avoids deep nesting and keeps the main logic clear and linear.