0
0
iOS Swiftmobile~20 mins

Codable protocol for JSON parsing in iOS Swift - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Codable Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
📝 Syntax
intermediate
2: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)?
A
class Person: Codable {
  var name: String
  var age: Int
}
B
struct Person {
  var name: String
  var age: Int
  func encode() {}
}
C
}
tnI :ega rav  
gnirtS :eman rav  
{ elbadoC :nosreP tcurts
D
struct Person: Codable {
  var name: String
  var age: Int
}
Attempts:
2 left
💡 Hint
Remember, Codable works with structs and classes, but types must match JSON keys exactly.
ui_behavior
intermediate
2: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}"
A
let data = jsonString.data(using: .utf8)!
let person = try JSONDecoder().decode(Person.self, from: data)
Blet person = try JSONDecoder().decode(Person.self, from: jsonString)
C
let data = jsonString.data(using: .ascii)!
let person = try JSONDecoder().decode(Person.self, from: data)
D
let data = jsonString.data(using: .utf8)!
let person = JSONDecoder().decode(Person.self, from: data)
Attempts:
2 left
💡 Hint
You must convert the JSON string to Data using UTF-8 encoding before decoding.
lifecycle
advanced
2: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?
A
struct Person: Codable {
  var name: String
  var age: Int = 0
}
B
struct Person: Codable {
  var name: String
  var age: String?
}
C
struct Person: Codable {
  var name: String
  var age: Int?
}
D
struct Person: Codable {
  var name: String
  var age: Int
}
Attempts:
2 left
💡 Hint
Make the property optional to handle missing keys gracefully.
navigation
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?
A
struct Person: Codable {
  var full_name: String
  var age: Int
}
B
struct Person: Codable {
  var name: String
  var age: Int
  enum CodingKeys: String, CodingKey {
    case name = "full_name"
    case age
  }
}
C
struct Person: Codable {
  var name: String
  var age: Int
  init(from decoder: Decoder) throws {
    let container = try decoder.container(keyedBy: CodingKeys.self)
    name = try container.decode(String.self, forKey: .full_name)
    age = try container.decode(Int.self, forKey: .age)
  }
  enum CodingKeys: String, CodingKey {
    case full_name
    case age
  }
}
D
struct Person: Codable {
  var name: String
  var age: Int
  enum CodingKeys: String, CodingKey {
    case name
    case age
  }
}
Attempts:
2 left
💡 Hint
Use CodingKeys enum to map JSON keys to different property names.
🔧 Debug
expert
2: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)
AThe JSON is missing the required 'age' key, causing a decoding failure.
BThe JSON string is not valid UTF-8 encoded data.
CThe 'name' property is not optional but missing in JSON.
DThe struct must be a class to decode properly.
Attempts:
2 left
💡 Hint
Check if all non-optional properties have matching keys in JSON.