Bird
0
0

Given this struct and JSON, what will be the value of userName after decoding?

medium📝 Predict Output Q4 of 15
iOS Swift - Networking
Given this struct and JSON, what will be the value of userName after decoding?
struct User: Decodable {
  let userName: String
  enum CodingKeys: String, CodingKey {
    case userName = "user_name"
  }
}

JSON: {"user_name": "alice"}
ADecoding error
B"user_name"
Cnil
D"alice"
Step-by-Step Solution
Solution:
  1. Step 1: Map JSON key to struct property

    The custom key maps "user_name" in JSON to userName in Swift.
  2. Step 2: Decode JSON value for userName

    The JSON value "alice" is assigned to userName.
  3. Final Answer:

    "alice" -> Option D
  4. Quick Check:

    Custom key maps JSON value correctly [OK]
Quick Trick: Custom keys map JSON keys to Swift properties [OK]
Common Mistakes:
  • Expecting property name as value
  • Assuming decoding fails without exact key match
  • Confusing key names with values

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More iOS Swift Quizzes