【发布时间】:2019-07-26 07:55:55
【问题描述】:
这是我正在使用的代码,
struct CreatePostResponseModel : Codable{
var transcodeId:String?
var id:String = ""
enum TopLevelCodingKeys: String, CodingKey {
case _transcode = "_transcode"
case _transcoder = "_transcoder"
}
enum CodingKeys:String, CodingKey{
case id = "_id"
}
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: TopLevelCodingKeys.self)
if let transcodeId = try container.decodeIfPresent(String.self, forKey: ._transcode) {
self.transcodeId = transcodeId
}else if let transcodeId = try container.decodeIfPresent(String.self, forKey: ._transcoder) {
self.transcodeId = transcodeId
}
}
}
这里,transcodeId 由_transcode 或_transcoder 决定。
但我希望 id 和其余的键(不包括在这里)被自动解码。我该怎么做?
【问题讨论】:
-
您还必须手动解码其他键。
标签: ios swift codable decodable encodable