0

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?

3
  • please post your json Commented Jun 19, 2022 at 15:09
  • 2
    Don't use try?, use a proper do/try/catch to get the error that will tell you exactly what failed, and share also the JSON. We can't guess what's wrong with your code with the given content of your question. Maybe line 3 in second file? Commented Jun 19, 2022 at 15:17
  • Your question doesn't cover the whole scenario in my opinion. And i guess you made some mistakes in building models. But I usually go to this site https://app.quicktype.io/ and make a model from here and rename the structures. you can try and let me know your outcome Commented Jun 19, 2022 at 15:28

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.