Bird
0
0

What will be the output of this code snippet?

medium📝 Predict Output Q4 of 15
iOS Swift - Local Data Persistence
What will be the output of this code snippet?
let defaults = UserDefaults.standard
defaults.set(true, forKey: "isLoggedIn")
let loggedIn = defaults.bool(forKey: "isLoggedIn")
print(loggedIn)
ACompilation error
Btrue
Cnil
Dfalse
Step-by-Step Solution
Solution:
  1. Step 1: Store a Boolean value

    The code saves true for key "isLoggedIn" using set(_:forKey:).
  2. Step 2: Retrieve the Boolean value

    Using bool(forKey:) returns the stored value true.
  3. Final Answer:

    true -> Option B
  4. Quick Check:

    Stored bool retrieved correctly = true [OK]
Quick Trick: bool(forKey:) returns stored Boolean or false if missing [OK]
Common Mistakes:
  • Expecting nil instead of false default
  • Confusing bool(forKey:) with optional return
  • Forgetting to save before reading

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More iOS Swift Quizzes