【问题标题】:How to store Big Integer in Swift 4 Codable?如何在 Swift 4 Codable 中存储大整数?
【发布时间】:2018-08-30 10:35:29
【问题描述】:

我正在尝试使用可编码的 swift 4 自动解析 API。 API 中的某些字段是 Codable 不支持的大整数,Codable 不支持 Swift 4.NSNumber 并且 UInt64 很小以适应它。我尝试使用第三方库,并将我的变量设置为该类型的可编码变量,但这也不起作用。我试图制作一个自定义类或结构,它将在容器中仅使用一个值进行转换,但不知道如何使容器接受 Big Int 类型或将其转换为字符串。 我的代码如下所示。有什么解决办法吗?

import Foundation
import BigNumber

class PersonalizationLean:Codable {

    var hubId:String?
    var appId:UInt8?
    var nodeId:Int?
    var name:String?
    var ico:String?
    var icoBase64:String?
    var isBin:Bool?
    var lastModifiedAt:Int?
    var shouldShowInUi:Bool?
    var applianceType:String?
    var tags:[String]?
    var placeId:PlaceIdCodable?
    var roomId:String?
    var id:String?
    var key:String?


    enum CodingKeys:String,CodingKey {
        case hubId
        case appId
        case nodeId
        case name
        case ico
        case icoBase64
        case isBin
        case lastModifiedAt
        case shouldShowInUi
        case applianceType
        case tags
        case placeId
        case roomId
        case id
        case key
    }

//    required init(from decoder: Decoder) throws {
//        do {
//            let container = try decoder.container(keyedBy: CodingKeys.self)
//            self.hubId = try container.decode(String.self, forKey: .hubId)
//            self.appId = try container.decode(UInt8.self, forKey: .appId)
//            self.nodeId = try container.decode(Int.self, forKey: .nodeId)
//            self.name = try container.decode(String.self, forKey: .name)
//            self.ico = try container.decode(String.self, forKey: .ico)
//            self.icoBase64 = try container.decode(String.self, forKey: .icoBase64)
//            self.isBin = try container.decode(Bool.self, forKey: .isBin)
//            self.lastModifiedAt = try container.decode(Int.self, forKey: .lastModifiedAt)
//            self.shouldShowInUi = try container.decode(Bool.self, forKey: .shouldShowInUi)
//            self.applianceType = try container.decode(String.self,forKey: .applianceType)
//            self.tags = try container.decode([String].self,forKey: .tags)
//
//
//        }catch {
//            print(error)
//        }
//    }

}

class PlaceIdCodable:Codable {
    var placeId:String?

    required init(from decoder:Decoder) throws {
        do  {
            let container = try decoder.singleValueContainer()
            let placeIdBig = try container.decode(BInt.self) //this gives error
        }catch {
            print(error)
        }

    }

}

我使用的库是BigNumber

【问题讨论】:

  • 整数有多大?
  • @MartinR 示例是这个 2.7924224107526434141341370365e+28
  • 我给的东西来自邮递员。实际值为 27592653341843742185735257620。我只是想表明即使 Int64 也无法处理。

标签: ios swift swift4 biginteger codable


【解决方案1】:

使用派生自NSDecimalNumber 的内置Decimal。采用Codable

【讨论】:

    【解决方案2】:

    BInt 不符合 Codable 或 Decodable

    在这里使用它应该确认提到的协议

    extension BInt: Decodable {
        public init(from decoder: Decoder) throws {
            // Initialization goes here
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2017-12-05
      • 2017-11-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多