Home > Enterprise >  decoding key-value fails the parsing in swift
decoding key-value fails the parsing in swift

Time:06-20

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

how can I sterilize?

CodePudding user response:

Your nativeName key is optional

let nativeName: [String:NativeName]?

Also delete all enum CodingKeys: String, CodingKey { content in your case there is no need for it at all

  • Related