如何使用自定义对象数组对枚举进行编码

安德鲁·诺维耶洛

提前感谢您的帮助!我对 SwiftUI 比较陌生,并且一直在努力使用自定义对象数组对枚举进行编码。这是代码:

struct ChartData: Codable {
    var data: DataType
...
    private enum CodingKeys : String, CodingKey { case id, title, type, info, label_x, label_y, last_update, data }

    func encode(to encoder: Encoder) throws {
       var container = encoder.container(keyedBy: CodingKeys.self)
...
        try container.encode(self.data, forKey: .data)
}
}

enum DataType: Codable{
    case plot([ChartPlotData])
    case slice([ChartSliceData])
    case number([ChartNumberData])
}

struct ChartPlotData: Codable {
    var chart_id: String
    var order_plot: Int
    var label_plot: String?
    var points_x: [String]
    var points_y: [Double]
    
    private enum CodingKeys : String, CodingKey { case chart_id, order_plot, label_plot, points_x, points_y }
    
    init(from decoder: Decoder) throws {
        let container = try decoder.container(keyedBy: CodingKeys.self)
        self.chart_id = try container.decode(String.self, forKey: .chart_id)
        self.order_plot = try container.decode(Int.self, forKey: .order_plot)
        self.label_plot = try? container.decode(String.self, forKey: .label_plot)
        do{
            self.points_x = try container.decode([String].self, forKey:.points_x)
        }catch{
            let xs = try container.decode([Double].self, forKey:.points_x)
            self.points_x = xs.map { String($0) }
        }
        self.points_y = try container.decode([Double].self, forKey:.points_y)
    }
}

struct ChartSliceData: Codable {
    var chart_id: String
    var order_slice: Int
    var label_slice: String
    var value_slice: Double
}

struct ChartNumberData: Codable {
    var chart_id: String
    var number: Double
    var unit: String?
}

我试图在 UserDefaults 中缓存 JSON,以避免进行无关的 API 调用。使用 JSONEncoder,我得到了以下 JSON 片段(摘自更长的字符串):

            "data" : {
              "plot" : {
                "_0" : [
                  {
                    "label_plot" : "China",
                    "points_y" : [
                      0,
                      0,
                      0,
...

但是,我希望获得这样的编码:

            "data" : [
                  {
                    "label_plot" : "China",
                    "points_y" : [
                      0,
                      0,
                      0,
...

任何帮助将不胜感激!非常感谢!

乔金丹尼尔森

这可以通过在 CodingKeys 枚举中添加一个额外的项来解决,用于对用于DataType枚举的类型进行编码和解码,然后使用 aswitch对正确类型的数组进行编码和解码。

这是完整的 ChartData 结构,但为简洁起见删除了一些属性和代码

struct ChartData: Codable {
    var id: Int
    var title: String
    var data: DataType
   
    enum CodingKeys: String, CodingKey {
        case id, title, data, type
    }
   
    func encode(to encoder: Encoder) throws {
        var container = encoder.container(keyedBy: CodingKeys.self)
        try container.encode(id, forKey: .id)
        try container.encode(title, forKey: .title)
        
        switch data {
        case .number(let values): 
            try container.encode("number", forKey: .type)
            try container.encode(values, forKey: .data)
        case .plot(let values): 
            try container.encode("plot", forKey: .type)
            try container.encode(values, forKey: .data)
        case .slice(let values): 
            try container.encode("slice", forKey: .type)
            try container.encode(values, forKey: .data)
        }
    }
    
    init(from decoder: Decoder) throws {
        var container = try decoder.container(keyedBy: CodingKeys.self)
        id = try container.decode(Int.self, forKey: .id)
        title = try container.decode(String.self, forKey: .title)
        
        let type = try container.decode(String.self, forKey: .type)
        switch type {
        case "number":
            let values = try container.decode([ChartNumberData].self, forKey: .data)
            data = .number(values)
                // case ...
        default:
            fatalError("Unsupported type for DataType: \(type)")
        }
    }
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何使用自定义管道在Angular中按属性值对对象数组进行排序?

如何使用Java 8中的自定义Comparator接口对对象数组进行排序?

根据kotlin中的枚举对自定义对象进行排序

可编码的自定义枚举类型

如何返回自定义对象数组?

使用数组的自定义对象

如何在 Java 中使用自定义值进行枚举?

如何在Swift 3中按自定义状态值对自定义对象数组进行排序?

如何使用自定义字母集进行 base64 编码?

Swift Codable-如何编码自定义数组

在Android中对自定义对象的数组列表进行排序

按日期快速对自定义对象数组进行排序

按自定义顺序对对象数组进行排序

SwiftUI 按 id 对自定义对象数组进行排序

根据动态条件对自定义对象数组进行排序

Swift如何按属性值对自定义对象数组进行排序

如何在Java数组中按字母顺序对自定义对象进行排序?

Javascript自定义按年份使用“前一年”对对象数组进行排序

使用三个级别的自定义排序标准对对象数组进行排序

构造一个自定义对象,其属性将按定义顺序进行枚举

如何使用自定义订单属性对枚举排序?

如何展平自定义对象数组的数组 [[CustomModel?]]?

如何将自定义对象序列化/编码为字节/字节数组?

您如何编写包含另一种类型的自定义对象数组的自定义对象,以在javascript中进行存储?

Spring Boot - 使用自定义对象数组 JSON 序列化自定义对象

如何使用 Python 解码 Angular 的自定义 HTML 编码

Python如何使用自定义编码实现排序?

如何使用数组使用参数按自定义顺序进行查询?

如何对带有自定义对象的NSMutableArray进行排序?