简体   繁体   中英

JSONDecoder can't decode on real device when it runs fine on simulator

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.

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.

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
   ....
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM