简体   繁体   English

如何在 Swift 4 Codable 中存储大整数?

[英]How to store Big Integer in Swift 4 Codable?

I am trying auto parsing an API with swift 4 codable.我正在尝试使用 swift 4 codable 自动解析 API。 Some of the fields in the API are big integer which Codable does not support nor Swift 4.NSNumber is not supported by Codable and UInt64 is small for it to fit. API 中的一些字段是 Codable 不支持的大整数,而且 Swift 4.NSNumber 不受 Codable 支持,UInt64 很小,无法适应。 I tried with a thrid party library and made my variable within the codable to that type,but that also did not work.我尝试使用第三方库并将我的变量设置为该类型的可编码变量,但这也不起作用。 I tried to to make a custom class or struct which will do the conversion with only one value in container but don't know how to make the container accept Big Int type or convert it to string.我试图创建一个自定义类或结构,它将只使用容器中的一个值进行转换,但不知道如何使容器接受 Big Int 类型或将其转换为字符串。 My code is below like this.我的代码如下所示。 Is there any solution to it?有什么解决办法吗?

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

    }

}

The library I am using is BigNumber我使用的库是BigNumber

Use built-in Decimal which derives from NSDecimalNumber .使用从NSDecimalNumber派生的内置Decimal It adopts Codable它采用Codable

BInt do not conform to Codable OR Decodable BInt不符合Codable OR Decodable

to use it here it should confirm to mentioned protocol在这里使用它应该确认提到的协议

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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