Daily iOS
Networking

Unstable response schemas

You're decoding responses into the model below with Codable, but the server includes the nickname field in some responses and leaves it out of others entirely. What happens with the current code?

struct Profile: Decodable {
    let id: Int
    let nickname: String
    let tags: [String]
}
How would an interviewer hear this answer? You'll see whether they'd move on, dig in, or say no, and which sentences would trip you up. Your score doesn't change.
See the short answer

Because nickname is non-optional, a missing field throws keyNotFound and decoding of the whole Profile fails. If the response is an array, one element takes the whole array down.

Share a question