Bird
0
0

[String: Any], let message = json["message"] as?

medium📝 Predict Output Q5 of 15
iOS Swift - Networking
Given this Swift code snippet, what will be printed if the server returns a JSON with a "message" key? let data = ... // JSON data from server if let json = try? JSONSerialization.jsonObject(with: data) as? [String: Any], let message = json["message"] as? String { print(message) } else { print("No message") }
AThe value of the "message" key from the JSON
BNo message
CAn error message about JSON parsing
DNothing will print
Step-by-Step Solution
Solution:
  1. Step 1: Analyze JSON parsing logic

    The code tries to parse JSON and extract the "message" string key.
  2. Step 2: Determine output if "message" key exists

    If the key exists, it prints its value; otherwise, it prints "No message".
  3. Final Answer:

    The value of the "message" key from the JSON -> Option A
  4. Quick Check:

    JSON "message" key exists = print value [OK]
Quick Trick: If JSON key exists, print its value; else print fallback [OK]
Common Mistakes:
  • Confusing optional binding with forced unwrapping
  • Expecting error print without catch block
  • Assuming no output if key missing

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More iOS Swift Quizzes