小编ohB*_*Boy的帖子

使用 Codable 使用相同的密钥解码不同的类

我正在使用提供 2 个 JSON URL 的 API。每个 URL 都包含一个嵌套容器,该容器具有属于同一类和对象的不同属性。

JSON 网址 1

{
  "last_updated": 1535936629,
  "xyz": 5,
  "data": {
    "dataList": [
      {
        "id": "42",
        "a1": "a1value",
        "a2": "a2value",
      },
      // ,,,
    ]
  }
}
Run Code Online (Sandbox Code Playgroud)

JSON 网址 2

{
  "last_updated": 1536639996,
  "xyz": 5,
  "data": {
    "dataList": [
      {
        "id": "42",
        "a3": "a3value",
        "a4": "a4value",
      },
      // ,,,
    ]
  }
}
Run Code Online (Sandbox Code Playgroud)

我想使用这些 JSON URLS 使用嵌套dataList列表中的项目创建单个 Codable CustomClass 对象,因此我创建了一个Feed结构来处理这 2 个 JSON 文件。

Feed.swift

import Foundation

Struct Feed: Decodable …
Run Code Online (Sandbox Code Playgroud)

class ios swift codable

0
推荐指数
1
解决办法
1756
查看次数

标签 统计

class ×1

codable ×1

ios ×1

swift ×1