Bird
0
0

What is the issue with this custom decoder configuration in Swift?

medium📝 Debug Q6 of 15
iOS Swift - Networking
What is the issue with this custom decoder configuration in Swift?
struct Product: Decodable {
  let id: Int
  let name: String
  enum CodingKeys: String, CodingKey {
    case id
    case productName = "name"
  }
}
AThe property name 'name' does not match the coding key 'productName'
BThe enum CodingKeys should not have raw type String
CThe case 'productName' should be 'name' to match the property
DThe struct is missing an explicit <code>init(from:)</code> initializer
Step-by-Step Solution
Solution:
  1. Step 1: Match property names to coding keys

    Each case in CodingKeys must correspond to a property name.
  2. Step 2: Identify mismatch

    The property is named 'name' but the coding key case is 'productName'.
  3. Step 3: Correct the case name

    Change case productName = "name" to case name = "name".
  4. Final Answer:

    The case 'productName' should be 'name' to match the property -> Option C
  5. Quick Check:

    CodingKeys cases must match property names [OK]
Quick Trick: CodingKeys cases must match property names exactly [OK]
Common Mistakes:
  • Using different case names than properties
  • Assuming raw value changes case name
  • Omitting CodingKeys enum entirely

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More iOS Swift Quizzes