【发布时间】:2022-06-16 23:16:58
【问题描述】:
我正在尝试解码由关联值组成的枚举。我正在尝试以下方法,但它不断抛出异常。
let jsonString = """
{
"route": "petDetails"
}
"""
let jsonData = jsonString.data(using: .utf8)
struct Post: Decodable {
let route: Route
}
enum Route: Decodable, Equatable {
case petDetails(String)
init?(rawValue: String) {
switch rawValue {
case "petDetails":
self = .petDetails("")
default:
return nil
}
}
private enum CodingKeys: String, CodingKey {
case petDetails
}
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
if let value = try? container.decode(String.self, forKey: .petDetails) {
self = .petDetails(value)
} else {
throw DecodingError.dataCorrupted(DecodingError.Context(codingPath: container.codingPath, debugDescription: "Data doesn't match"))
}
}
}
try! JSONDecoder().decode(Post.self, from: jsonData!)
我收到以下错误:
Fatal error: 'try!' expression unexpectedly raised an error: Swift.DecodingError.typeMismatch(Swift.Dictionary<Swift.String, Any>, Swift.DecodingError.Context(codingPath: [CodingKeys(stringValue: "route", intValue: nil)], debugDescription: "Expected to decode Dictionary<String, Any> but found a string/data instead.", underlyingError: nil))
任何想法我缺少什么?
【问题讨论】:
-
你为什么选择
enum?您的数据可以是多种类型,例如route键的 Int 和 String 吗? -
Post 有一个路由属性,它是枚举。 Enum 是 Route,它可以有许多不同的情况。我从 petDetails 开始,但它可以有 petProfile、petListing 等。
-
你能分享一个假人
JSON2-3 箱吗?这将有助于我们了解更多。 -
这是一个更大的应用程序的一部分。我只是想为应用程序的一小部分解决这个解码错误。
-
好的,你的
JSON应该是这样的:route: {"pet_details" : "abc" }而不是route: "petDetails。它期望使用该枚举解码字典,并且您提供了String,这就是它抛出错误的原因。