Bird
0
0

Given this JSON:

medium📝 Predict Output Q4 of 15
iOS Swift - Networking
Given this JSON:
{"id": 1, "name": "Alice"}

And this struct:
struct Person: Codable { var id: Int; var name: String }

What will be the value of person.name after decoding?
A"id"
B"Alice"
C1
Dnil
Step-by-Step Solution
Solution:
  1. Step 1: Match JSON keys to struct properties

    The JSON key "name" matches the struct property name, so decoding sets person.name to "Alice".
  2. Step 2: Confirm decoding behavior

    Since the JSON contains "name": "Alice", decoding assigns that string to person.name.
  3. Final Answer:

    "Alice" -> Option B
  4. Quick Check:

    Decoded name = "Alice" [OK]
Quick Trick: JSON keys map to struct properties by name [OK]
Common Mistakes:
  • Confusing keys with values
  • Expecting nil when JSON has value
  • Mixing property names with values

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More iOS Swift Quizzes