Map JSON keys to differently named Codable properties
WHAT IT TESTS: Codable key customization. OUTLINE: declare a nested CodingKeys enum mapping userID to the raw value "user_id", or set the decoder's keyDecodingStrategy to convertFromSnakeCase for blanket conversion.
WHAT IT TESTS: whether you know the idiomatic ways to bridge JSON naming and Swift naming. ANSWER OUTLINE: add a private CodingKeys enum conforming to String and CodingKey inside the type, listing every property and giving userID the raw value "user_id" so synthesized coding maps it; alternatively set decoder.keyDecodingStrategy to .convertFromSnakeCase, which automatically converts snake_case keys to camelCase for the whole payload.
Read the original → interview
- #ios
- #swift
- #codable
- #json
- #codingkeys
Get five bites like this every day.
Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.