Bird
0
0

Which of the following is the correct way to define a custom key mapping in a Swift Decodable struct?

easy📝 Syntax Q12 of 15
iOS Swift - Networking
Which of the following is the correct way to define a custom key mapping in a Swift Decodable struct?
ADeclare properties as <code>lazy var</code>
BUse an enum conforming to <code>CodingKey</code> with cases matching JSON keys
COverride the <code>init()</code> method without parameters
DUse <code>@IBOutlet</code> annotations on properties
Step-by-Step Solution
Solution:
  1. Step 1: Recall how to customize keys in Decodable

    Swift uses an enum conforming to CodingKey to map JSON keys to property names.
  2. Step 2: Identify the correct syntax

    The enum cases represent the JSON keys, allowing custom mapping.
  3. Final Answer:

    Use an enum conforming to CodingKey with cases matching JSON keys -> Option B
  4. Quick Check:

    Custom keys = enum CodingKey [OK]
Quick Trick: Custom keys use enum CodingKey with cases [OK]
Common Mistakes:
  • Trying to override init() without parameters
  • Using lazy vars for decoding
  • Confusing UI annotations with decoding

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More iOS Swift Quizzes