有没有办法将Decodable
与注入的属性一起使用?
final class Score: Decodable {
let value: Int?
let uniqueId: String
convenience init(from decoder: Decoder/*, uniqueId: String*/) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
try container.decodeIfPresent(Int.self, forKey: .value).flatMap { value = $0 }
// self.uniqueId = uniqueId
[... other properties parsing ...]
}
}
调用示例:
final class Exam {
let identifier: Int
let scores: [Score]
convenience init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
identifier = try container.decode(Int.self, forKey: .identifier)
scores = try container.decode([Score].self, forKey: .scores)
// I need to pass Exam's identifier to `score` on init, because it will generate Score's `uniqueId `
[... other properties parsing ...]
}
}
这将以缺少uniqueId
的错误结束,我需要在初始化后拥有它,但它不在 JSON 中。由于它是标识符,因此将其设置为可选并在外部设置不是处理它的正确方法。
我很想按照上面评论的方式注入它,但是怎么做呢?
无法扩展初始化器,因为它是间接调用的,并且没有提供 API 来扩展它。因此,有几种方法可以绕过它:
- 最佳:如果可能,将值注入解码器的
userInfo
。
为 - 响应创建单独的类,为模型创建单独的类。下面是示例。
- 使用普通
JSONSerialization
而不是Decodable
。 - @JoakimDanielson建议的那样,在默认初始化器中创建随机标识符。问题是它不可重现,因此,如果您要将其保存到数据库,您将始终覆盖数据,因为每次解析的 ID 会有所不同。
方法 2 的示例:
final class ScoreResponse: Decodable {
let value: Int?
convenience init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
try container.decodeIfPresent(Int.self, forKey: .value).flatMap { value = $0 }
[... other properties parsing ...]
}
}
final class Score {
let value: Int?
let uniqueId: String
convenience init(from response: ScoreResponse, uniqueId: String) {
self.value = response.value // etc with other properties
self.uniqueId = uniqueId
}
}
final class Exam: Decodable {
let identifier: String
let scores: [Score] = []
convenience init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
identifier = try container.decode(String.self, forKey: .identifier)
try container.decodeIfPresent([ScoreResponse].self, forKey: .scores).forEach {
scores.append({ Score(from: $0, uniqueId: identifier) })
}
}