Swift JSON 解码字典失败

问题描述

我有一个如下所示的结构:

struct ItemList: Decodable {
    var items: [UUID: Int]
}

我得到的示例 JSON 数据是:

{
    "items": {
        "b4f8d2fa-941f-4f9a-a98c-060bbd468575": 418226428193,"81efa661-4845-491b-8bf4-06d5dff1d5f8": 417639857722
    }
}

现在,当我尝试解码上述数据时,出现了一个有趣的错误。显然,我不是在解码数组,而且很明显所有内容都指向字典。

try JSONDecoder().decode(ItemList.self,from: data)
// typeMismatch(
//     Swift.Array<Any>,//     Swift.DecodingError.Context(
//         codingPath: [
//             CodingKeys(stringValue: "items",intValue: nil)
//         ],//         debugDescription: "Expected to decode Array<Any> but found a dictionary instead.",//         underlyingError: nil
//     )
// )

所以我进行了试验并将 [UUID: Int] 更改为 [String: Int],这确实使这项工作有效,几乎让我认为错误与数组/字典无关,而是与 UUID/String 相关。所以我也做了下面的测试,从来没有失败过。

let list = try JSONDecoder().decode(ItemList.self,from: data)
for (key,value) in list.items {
    // This will never print `nil`
    print(UUID(uuidString: key))
}

所以我的问题是,为什么我在解码时会收到这个奇怪的 typeMismatch 错误,为什么当我将 UUID 更改为 String 时它会起作用,因为它显然可以正确解码?

解决方法

this 文章很好地解释了为什么会发生这种情况以及您可以采取什么措施。简短摘要:

  • Swift 将字典编码为数组,其中每个值都跟在一个键之后
  • 您可以使用多种方法来解决这个问题:

a) 使用 dict 的数组表示来适应 swift 的方式
b) 使用 StringInt 作为您的键类型
c) 使用自定义解码器
d) 使用 RawRepresentable-Protocol