Bird
0
0

Given this JSON string:

medium📝 Predict Output Q13 of 15
iOS Swift - Networking
Given this JSON string:
{"id": 10, "title": "Book"}

And this Swift struct:
struct Item: Codable { var id: Int; var title: String }

What will be the value of item.title after decoding?
A"id"
B"Book"
C10
Dnil
Step-by-Step Solution
Solution:
  1. Step 1: Match JSON keys to struct properties

    The JSON has keys "id" and "title" matching the struct's properties.
  2. Step 2: Decode JSON to struct

    Decoding will assign the value "Book" to title and 10 to id.
  3. Final Answer:

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

    Decoded title = "Book" [OK]
Quick Trick: JSON keys match struct properties exactly [OK]
Common Mistakes:
  • Confusing property names with values
  • Expecting numeric value for title
  • Assuming decoding fails and returns nil

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More iOS Swift Quizzes