1

I use an API to parse some data - this works. But now the api uses names like "d/a" and "test%" and I can't create a codable struct with this symbols. Is there a way to use them?

Thanks in advance

Nxt97
  • 151
  • 2
  • 9

1 Answers1

6

You can remap your keys. Here is a basic example:

struct MyObject: Codable {
    var name: String
    var da: String
    var test: String

    enum CodingKeys: String, CodingKey {
        case name
        case da = "d/a"
        case test = "test%"
    }
}

You can read more about it in the documentation here

Keep in mind though that you will have to manually include all the properties that you want to be encoded/decoded and that "The names of the enumeration cases should match the names you've given to the corresponding properties in your type"

Alladinian
  • 34,483
  • 6
  • 89
  • 91