Challenge - 5 Problems
Codable Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
📝 Syntax
intermediate2:00remaining
Correct Codable struct syntax
Which option correctly defines a Swift struct that conforms to Codable for parsing JSON with keys
name (String) and age (Int)?Attempts:
2 left
💡 Hint
Remember,
Codable works with structs and classes, but types must match JSON keys exactly.✗ Incorrect
Option D correctly defines a struct with matching property types and conforms to Codable. Option D lacks Codable conformance. Option D uses a class which is valid but less common for simple models. Option D has wrong type for age (String instead of Int).
❓ ui_behavior
intermediate2:00remaining
Decoding JSON with Codable
Given this JSON string:
{"name":"Alice","age":30}, which option correctly decodes it into a Person struct using JSONDecoder?iOS Swift
struct Person: Codable {
var name: String
var age: Int
}
let jsonString = "{\"name\":\"Alice\",\"age\":30}"Attempts:
2 left
💡 Hint
You must convert the JSON string to Data using UTF-8 encoding before decoding.
✗ Incorrect
Option A correctly converts the string to Data with UTF-8 and uses try to decode. Option A tries to decode from String directly, which is invalid. Option A uses ASCII encoding which may fail for some characters. Option A misses try keyword causing compile error.
❓ lifecycle
advanced2:00remaining
Handling missing JSON keys during decoding
If a JSON response sometimes omits the
age key, how should you define the Person struct to decode successfully without errors?Attempts:
2 left
💡 Hint
Make the property optional to handle missing keys gracefully.
✗ Incorrect
Option C uses an optional Int for age, so decoding succeeds even if age is missing. Option C requires age always present. Option C sets a default but decoding will fail if key is missing unless you implement custom init. Option C uses wrong type for age.
advanced
2:00remaining
Custom key mapping with Codable
Given JSON
{"full_name":"Bob Smith","age":25}, how do you map full_name to a Swift property name in a Codable struct?Attempts:
2 left
💡 Hint
Use CodingKeys enum to map JSON keys to different property names.
✗ Incorrect
Option B correctly maps JSON key "full_name" to property "name" using CodingKeys. Option B uses property name matching JSON key but does not rename. Option B tries manual decoding but uses wrong key in CodingKeys for name property. Option B does not map keys so decoding fails.
🔧 Debug
expert2:00remaining
Identify decoding error cause
This code throws a decoding error. What is the cause?
struct Person: Codable {
var name: String
var age: Int
}
let json = "{\"name\":\"Eve\"}" // age key missing
let data = json.data(using: .utf8)!
let person = try JSONDecoder().decode(Person.self, from: data)
iOS Swift
struct Person: Codable {
var name: String
var age: Int
}
let json = "{\"name\":\"Eve\"}"
let data = json.data(using: .utf8)!
let person = try JSONDecoder().decode(Person.self, from: data)Attempts:
2 left
💡 Hint
Check if all non-optional properties have matching keys in JSON.
✗ Incorrect
The decoding fails because 'age' is a non-optional Int property but the JSON does not include it. This mismatch causes a decoding error. The JSON is valid UTF-8 and contains 'name'. The struct can be a struct or class.