Ant*_*hko 1 nsdecimalnumber swift codable jsondecoder
有没有办法告诉 JSONDecoder 将传入的小数转换为字符串?
public struct Transaction: Decodable
{
public let total: NSDecimalNumber?
enum CodingKeys: String, CodingKey {
case total = "AMOUNT"
}
public init(from decoder: Decoder) throws
{
let values = try decoder.container(keyedBy: CodingKeys.self)
let total = try values.decode(Decimal.self, forKey: .total)
self.total = NSDecimalNumber(decimal: total);
}
}
Run Code Online (Sandbox Code Playgroud)
考虑当 AMOUNT 类似于 397289527598234759823475455622342424358363514.42 时会发生什么
我想使用我拥有的代码,我会得到 nonConformingFloatDecodingStrategy 异常而无法从中恢复或失去精度。
围绕愚蠢的 swift Decimal 的斗争被记录在了所有的地方,特别是在这里:
| 归档时间: |
|
| 查看次数: |
266 次 |
| 最近记录: |