是否可以在调用decode
之后但在进行类型检查之前执行代码?
let o7 = try decoder.decode(Organization.self, from: o6)
o7.configure()
return o7.save(on: request.db).flatMap { page in
return request.eventLoop.future("done")
}
抛出错误:
发现:typeMismatch (Swift.Dictionary<迅速。字符串,App.Event>Context(codingPath: [ModelCodingKey(stringValue: "events", intValue: nil)], debugDescription: "Could not decode property"keyNotFound(ModelCodingKey(stringValue: "events" intValue: nil), Swift.DecodingError。Context(codingPath: [], debugDescription: " key没有关联的值ModelCodingKey(stringValue: "events", intValue: nil) ("events").", underlyingError: nil)))))
o7
尚未符合类型要求,但o7.configure()
完成了所需的步骤,是否可以"询问"?调用configure
后检查类型?
这是JSON:{"name": "xxx"}
这里是类型:
final class Organization: Content {
var name: String?
var events: [String: Event]
}
如您所见,我需要初始化events
以避免typeemismatch错误。我在configure
方法中做。
尝试添加CodingKeys,这将排除"var events"从json可解码的。
final class Organization: Codable {
var name: String?
var events: [String: Event] = [:]
private enum CodingKeys: String, CodingKey {
case name
}
}