【发布时间】:2023-04-03 19:14:01
【问题描述】:
使用 Xcode 10.2 和 iOS 12.x,我们能够从 json 字符串中提取 Decimal。对于 Xcode 11.1 和 iOS 13.1,它会抛出异常
应解码 Double,但找到了一个字符串/数据。
class MyClass : Codable {
var decimal: Decimal?
}
然后尝试解析它
let json = "{\"decimal\":\"0.007\"}"
let data = json.data(using: .utf8)
let decoder = JSONDecoder()
decoder.nonConformingFloatDecodingStrategy = .convertFromString(positiveInfinity: "s1", negativeInfinity: "s2", nan: "s3")
do {
let t = try decoder.decode(MyClass.self, from: data!)
} catch {
print(error)
}
如果我将 json 字符串更改为
let json = "{\"decimal\":0.007}"
它有效,但我们又一次失去了精确度。有什么想法吗?
【问题讨论】:
-
您是从服务器接收字符串还是双精度数?
-
我都收到了,但现在字符串是有问题的。将双精度解析为十进制仍然可以。