Bird
0
0

You want to decode JSON where the key names differ from your struct property names. How can you handle this with Codable?

hard📝 Application Q8 of 15
iOS Swift - Networking
You want to decode JSON where the key names differ from your struct property names. How can you handle this with Codable?
AImplement <code>CodingKeys</code> enum to map JSON keys to property names
BRename the JSON keys manually before decoding
CUse a different struct for each JSON key variant
DCodable automatically matches keys ignoring names
Step-by-Step Solution
Solution:
  1. Step 1: Understand key mapping in Codable

    Codable allows custom key mapping by defining a CodingKeys enum inside the struct.
  2. Step 2: Evaluate other options

    Renaming JSON manually or using multiple structs is inefficient; Codable does not ignore key names automatically.
  3. Final Answer:

    Implement CodingKeys enum to map JSON keys to property names -> Option A
  4. Quick Check:

    Use CodingKeys to map keys [OK]
Quick Trick: Use CodingKeys enum to rename JSON keys [OK]
Common Mistakes:
  • Expecting automatic key matching
  • Manually renaming JSON outside Codable
  • Creating multiple structs unnecessarily

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More iOS Swift Quizzes