1

I have a JSON data pulled from API and one of the key is "24h_volume".

When I try to put 24h_volume as a constant inside my Decodable struct giving me error:

Expected a digit after integer literal prefix.

From what I understand, Swift syntax does not allow variables names starting with numbers.

What is the simplest way to surpass this restriction and successfully pull the data from the source?

Paulo Mattos
  • 18,845
  • 10
  • 77
  • 85
Vetuka
  • 1,523
  • 1
  • 24
  • 40
  • What is the problem? The Swift properties need not have the same name as the JSON dictionary keys. – Martin R Oct 28 '17 at 12:42
  • When I decode the JSON in Swift 4 it assigns all the values to their relative names in the Class. So when I give exact names to the constants in the class, I get their values from downloaded data. – Vetuka Oct 28 '17 at 12:46
  • You did not tell that you are using Swift 4 or JSONDecoder ... – Martin R Oct 28 '17 at 12:48
  • I am sorry, I fixed it on the edit. – Vetuka Oct 28 '17 at 12:59

1 Answers1

3

Not clear from your question but, if you are using Swift 4 JSON decoding support, you can define a custom CodingKeys enumeration for that. For instance:

struct Data: Decodable {
    var volume24: String
    var xxx: String
    ...

    enum CodingKeys: String, CodingKey {
        case volume24 = “24h_volume”
        case xxx
        case ...
    }
}

You must then define a case for each property in your struct otherwise it will be ignored by JSONDecoder.

Paulo Mattos
  • 18,845
  • 10
  • 77
  • 85