Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

how can I sterilize?

>Solution :

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading