Challenge - 5 Problems
Custom Decoder Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
📝 Syntax
intermediate2:00remaining
Correct syntax for custom decoder init
Which option correctly implements a custom initializer for decoding a Swift struct with a nested container?
iOS Swift
struct User: Decodable {
let name: String
let age: Int
enum CodingKeys: String, CodingKey {
case info
}
enum InfoKeys: String, CodingKey {
case name
case age
}
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
let infoContainer = try container.nestedContainer(keyedBy: InfoKeys.self, forKey: .info)
name = try infoContainer.decode(String.self, forKey: .name)
age = try infoContainer.decode(Int.self, forKey: .age)
}
}Attempts:
2 left
💡 Hint
Remember to access the nested container using the correct key and key type.
✗ Incorrect
Option D correctly accesses the nested container 'info' using CodingKeys, then decodes 'name' and 'age' from InfoKeys. Other options either use wrong container types or keys.
❓ ui_behavior
intermediate1:30remaining
Effect of missing key in custom decoder
What happens if a required key is missing when decoding with a custom decoder initializer in Swift?
iOS Swift
struct Product: Decodable {
let id: Int
let name: String
enum CodingKeys: String, CodingKey {
case id
case name
}
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
id = try container.decode(Int.self, forKey: .id)
name = try container.decode(String.self, forKey: .name)
}
}
// JSON missing 'name' key
let jsonData = "{\"id\": 101}".data(using: .utf8)!Attempts:
2 left
💡 Hint
Think about how Swift Decodable handles missing required keys by default.
✗ Incorrect
When a required key is missing, decoding throws a DecodingError.keyNotFound error. It does not assign default or nil values automatically.
❓ lifecycle
advanced2:30remaining
Custom decoder with date decoding strategy
How can you configure a JSONDecoder to decode dates in a custom format when decoding a struct with a custom decoder initializer?
iOS Swift
struct Event: Decodable {
let title: String
let date: Date
enum CodingKeys: String, CodingKey {
case title
case date
}
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
title = try container.decode(String.self, forKey: .title)
date = try container.decode(Date.self, forKey: .date)
}
}
let decoder = JSONDecoder()
// Configure decoder here for custom date format
Attempts:
2 left
💡 Hint
Use a custom closure to parse the date string with your own format.
✗ Incorrect
Option A shows how to use a custom dateDecodingStrategy with a closure that parses the date string using a DateFormatter with a specific format. Other options use predefined strategies or an empty formatter.
advanced
2:00remaining
Decoding nested JSON with optional nested container
Given a JSON where a nested object may be missing, how do you safely decode an optional nested container in a custom decoder initializer?
iOS Swift
struct Profile: Decodable {
let username: String
let address: Address?
struct Address: Decodable {
let city: String
let zip: String
}
enum CodingKeys: String, CodingKey {
case username
case address
}
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
username = try container.decode(String.self, forKey: .username)
if container.contains(.address) {
let addressContainer = try container.nestedContainer(keyedBy: AddressCodingKeys.self, forKey: .address)
let city = try addressContainer.decode(String.self, forKey: .city)
let zip = try addressContainer.decode(String.self, forKey: .zip)
address = Address(city: city, zip: zip)
} else {
address = nil
}
}
enum AddressCodingKeys: String, CodingKey {
case city
case zip
}
}Attempts:
2 left
💡 Hint
Swift Decodable supports decoding optional nested objects directly.
✗ Incorrect
Option A is the simplest and safest way to decode an optional nested object by using decodeIfPresent. Options A and B work but are more verbose. Option A risks runtime error if key is missing.
🔧 Debug
expert3:00remaining
Identify the cause of decoding failure in custom decoder
Why does the following custom decoder initializer fail to decode the JSON correctly?
iOS Swift
struct Item: Decodable {
let id: Int
let tags: [String]
enum CodingKeys: String, CodingKey {
case id
case tags
}
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
id = try container.decode(Int.self, forKey: .id)
let tagsContainer = try container.nestedUnkeyedContainer(forKey: .tags)
tags = try tagsContainer.decode([String].self)
}
}
// JSON example: {"id": 5, "tags": ["swift", "ios"]}Attempts:
2 left
💡 Hint
Unkeyed containers decode elements one at a time, not whole arrays.
✗ Incorrect
nestedUnkeyedContainer returns a container to decode elements sequentially. You cannot decode an entire array from it directly. You must decode elements one by one or decode the array directly from the main container.