简体   繁体   English

解码键值在 swift 中解析失败

[英]decoding key-value fails the parsing in swift

I have these Models:我有这些模型:

struct City: Decodable {
    let famous: Flags
    let name: Name
}

struct Name: Decodable {
    var common , official : String
    var nativeName: [String:NativeName]
    
    enum CodingKeys: String, CodingKey {
        case common, official
        case nativeName = "nativeName"
    }

struct NativeName: Codable {
    let official, common: String
    
    enum CodingKeys: String, CodingKey {
        case official, common
    }

Decoding command:解码命令:

 let content = try? decoder.decode([Cities].self, from: data)

as soon as comment the var nativeName: [String:NativeName] line everyThing is okay只要评论 var nativeName: [String:NativeName] 行 everyThing 就可以了

how can I sterilize?我该如何消毒?

Your nativeName key is optional您的nativeName键是可选的

let nativeName: [String:NativeName]?

Also delete all enum CodingKeys: String, CodingKey { content in your case there is no need for it at all同时删除所有enum CodingKeys: String, CodingKey { content in your case 根本不需要它

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM