-2

I can easily decode the keys that are Camel Case:

struct TheData: Codable {
    let theNumber: Float
}

But I can not decode this version:

struct TheData: Codable {
    let TheNumber: Float
}

"TheNumber" is with first letter capitalized and Swift does allow constants starting with capital case. I have tried to lower case the first letter but it didn't work.

HangarRash
  • 7,314
  • 5
  • 5
  • 32
Viaxeen
  • 7
  • 4
  • The API result is like this: TheNumber: 0.3420129716396332 – Viaxeen Nov 27 '22 at 06:50
  • 1
    See https://stackoverflow.com/a/44396824/20287183 – HangarRash Nov 27 '22 at 06:51
  • 2
    Does this answer your question? [Decode a PascalCase JSON with JSONDecoder](https://stackoverflow.com/questions/60746366/decode-a-pascalcase-json-with-jsondecoder) – HangarRash Nov 27 '22 at 06:53
  • Yes thanks. I found the answer in: https://stackoverflow.com/questions/44396500/how-do-i-use-custom-keys-with-swift-4s-decodable-protocol/44396824#44396824 – Viaxeen Nov 27 '22 at 07:15

1 Answers1

0

Here is the solution:

struct TheData: Codable {
    let theNumber: Float
    private enum CodingKeys: String, CodingKey {
        case theNumber = "TheNumber"
    }
}

It is mentioned in: How do I use custom keys with Swift 4's Decodable protocol?

Viaxeen
  • 7
  • 4