与 Decodable 和 Object 类型不匹配

Type Mismatch with Decodable and Object

我在解析来自服务器的数据时遇到问题。我有 JSON,它有一个对象数组,像这样:

{
    "items": [
        {
            "itemType": {
                "id": 12,
                "tagId": "FCHA78558D"
            },
            "parts": [
                {
                    "partId": 52,
                    "manufacturer": "xxx"
                },
                {
                    "partId": 53,
                    "manufacturer": "xxx"
                },
                {
                    "partId": 54,
                    "manufacturer": "xxx"
                }
            ],
            "description": "yyy"
        },
        {
            "itemType": {
                "id": 13,
                "tagId": "FCHA755158D"
            },
            "parts": [
                {
                    "partId": 64,
                    "manufacturer": "xxx"
                },
                {
                    "partId": 65,
                    "manufacturer": "xxx"
                }
            ],
            "description": "zzz"
        }
    ]
}

我只想获得这一个对象数组,所以我这样实现class:

class User : Object, Decodable {
   var items = List<Equipment>()
}

在 Alamofire 中,我正在下载 JSON,将其解析为数据,然后在 do-catch 块中收到错误:

let items = try JSONDecoder().decode(User.self, from: receivedValue)

错误:

▿ DecodingError
  ▿ typeMismatch : 2 elements
    - .0 : Swift.Array<Any>
    ▿ .1 : Context
      ▿ codingPath : 2 elements
        - 0 : CodingKeys(stringValue: "items", intValue: nil)
        ▿ 1 : _JSONKey(stringValue: "Index 0", intValue: 0)
          - stringValue : "Index 0"
          ▿ intValue : Optional<Int>
            - some : 0
      - debugDescription : "Expected to decode Array<Any> but found a dictionary instead."
      - underlyingError : nil

这很奇怪,因为它肯定是一个对象数组。我尝试将我的项目 属性 设置为 String 以查看结果,然后我得到: - debugDescription : "Expected to decode String but found an array instead." 我有几次这个错误,但我总是设法找到解决方案。

我想你使用的是 List 条件符合 Decodable 从我对你的 的回答。我不完全理解为什么它在这种特定情况下不起作用,但我会调查一下。

到那时,您可以通过手动实现 init(from decoder:Decoder) 函数来进行解码。

class User : Object, Decodable {
    let items = List<Equipment>()

    private enum CodingKeys: String, CodingKey {
        case items
    }
    required convenience init(from decoder:Decoder) throws {
        self.init()
        let container = try decoder.container(keyedBy: CodingKeys.self)
        let itemsArray = try container.decode([Equipment].self, forKey: .items)
        self.items.append(objectsIn: itemsArray)
    }
}