如果某个类型不符合协议 Decodable,可以通过实现自定义的解码逻辑来解决这个问题。下面是一个示例代码:
struct Person {
var name: String
var age: Int
}
extension Person: Decodable {
enum CodingKeys: String, CodingKey {
case name
case age
}
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
name = try container.decode(String.self, forKey: .name)
// 通过容器的 decode 方法解码 age 属性,并将其转换为整数类型
if let ageString = try container.decodeIfPresent(String.self, forKey: .age),
let age = Int(ageString) {
self.age = age
} else {
// 如果 age 无法解码或转换为整数,则给 age 一个默认值
self.age = 0
}
}
}
// 测试解码
let json = """
{
"name": "John",
"age": "30"
}
"""
let jsonData = json.data(using: .utf8)!
let decoder = JSONDecoder()
do {
let person = try decoder.decode(Person.self, from: jsonData)
print(person) // 输出: Person(name: "John", age: 30)
} catch {
print("解码失败: \(error)")
}
在上面的代码中,我们定义了一个名为 Person 的结构体,并使其遵循了 Decodable 协议。由于 age 属性在 JSON 中是一个字符串类型,但在 Person 结构体中是一个整数类型,我们需要自定义解码逻辑。
在 init(from:) 方法中,我们首先通过 decoder.container(keyedBy:) 方法获取到一个包含了 JSON 键的容器。然后,我们使用容器的 decode 方法来解码 name 属性,因为它是一个字符串类型。
接下来,我们使用容器的 decodeIfPresent 方法来尝试解码 age 属性。如果 age 无法解码成字符串或无法转换为整数类型,则我们给 age 赋予一个默认值。
最后,我们使用 JSONDecoder 对象来解码 JSON 数据,并将结果赋给 person 常量。如果解码失败,则会抛出一个错误。