Home > Software design >  JSONDecoder can't decode on real device when it runs fine on simulator
JSONDecoder can't decode on real device when it runs fine on simulator

Time:06-06

I have a JSONDecoder that works on simulator, but it can't decode the same data when running on real device, please help. This error will happen when running on real device:

dataCorrupted(Swift.DecodingError.Context(
  codingPath: [_JSONKey(stringValue: "Index 0", intValue: 0), 
  CodingKeys(stringValue: "market_cap", intValue: nil)], 
  debugDescription: "Parsed JSON number <16541228553> does not fit in Int.", 
  underlyingError: nil))

I think 16541228553 should fit in Int? It fits when running on simulator. Thanks.

CodePudding user response:

There is very little information about the question but I guess the market_cap property in your model is Int.

Changing with Decimal would fix the problem

 class YourClass: Codable {
   var market_cap: Decimal
   ....
 }

CodePudding user response:

You note in the tags that this is an Apple Watch. I'm guessing it's a Series 3, which is a 32-bit platform. This is a 34-bit number, so it doesn't fit.

The Simulator does not emulate hardware. It's a 64-bit Mac app that links libraries that match the device SDK. So on Simulator, it doesn't overflow.

  • Related