Swift 4 decoding doubles from JSON

后端 未结 1 1336
旧巷少年郎
旧巷少年郎 2021-01-26 02:31

I though I had this concept nailed!

I am sending a JSON which contains a double.

{\"elementName\":\"Security:Driver\",\"element_Cost\":\"650\"}


        
相关标签:
1条回答
  • 2021-01-26 03:30

    "650" is a string, not a number.

    You can parse it like this

    let elementCostString = try values.decode(String.self, forKey: .elementCost)
    elementConst = Double(elementCostString) ?? 0
    

    Or change it to be a String on your model, whichever works better for you.

    0 讨论(0)
提交回复
热议问题