Bird
0
0

What will this Swift code print if run offline after saving data?

medium📝 Predict Output Q13 of 15
iOS Swift - Local Data Persistence
What will this Swift code print if run offline after saving data?
UserDefaults.standard.set("Swift", forKey: "language")
let saved = UserDefaults.standard.string(forKey: "language")
print(saved ?? "No data")
ASwift
BNo data
Cnil
DError
Step-by-Step Solution
Solution:
  1. Step 1: Save string to UserDefaults

    The code saves "Swift" with key "language" locally on the device.
  2. Step 2: Retrieve saved string and print

    It then retrieves the string for key "language" and prints it. Since it was saved, it prints "Swift".
  3. Final Answer:

    Swift -> Option A
  4. Quick Check:

    Saved data retrieval = "Swift" [OK]
Quick Trick: Saved UserDefaults data prints the stored value [OK]
Common Mistakes:
  • Assuming data is lost offline
  • Expecting nil or error instead of saved value
  • Confusing optional unwrapping

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More iOS Swift Quizzes