gpt4 book ai didi

swift - 使 Enum 符合 Codable 并映射到模型

转载 作者:行者123 更新时间:2023-11-28 14:05:04 25 4
gpt4 key购买 nike

我正在访问公共(public)交通 API 并使用 Codable 和 Alamofire 5 将响应映射到模型。

它似乎大部分都有效,但 API 没有正确规范化,这意味着我正在为相同的属性获取不同类型的数据(总线可以是 Int 或 String 等...)

我正在尝试将车辆类型属性映射到如下所示的枚举:

enum VehiculeType {

case bus
case trolleybus
case tram

init?(rawValue: String) {
switch rawValue {
case "AB", "ABA", "ABAA":
self = .bus
break
case "TBA", "TBAA":
self = .trolleybus
case "TW6", "TW7", "TW2":
self = .tram
default: return nil
}
}
}

这是到目前为止正确解码的模型。

struct Departure: Codable {

// let lineCode: String
let destination: String
let waitingTime: Int
let waitingMilliseconds: Int
let reroute: String
// let vehiculeType: VehiculeType?


init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
destination = try container.decode(String.self, forKey: .destination)
waitingTime = try container.decode(Int.self, forKey: .waitingTime, transformFrom: String.self) ?? 0
waitingMilliseconds = try container.decode(Int.self, forKey: .waitingMilliseconds)
reroute = try container.decode(String.self, forKey: .reroute)
// vehiculeType = try container.decodeIfPresent(String.self, forKey: .vehiculeType, transformFrom: String.self) // This does not work. Correct implementation needed here
}
}

extension Departure {
enum CodingKeys: String, CodingKey {
case destination = "destination"
case waitingTime = "attente"
case waitingMilliseconds = "attenteMilli"
case reroute = "deviation"
// case vehiculeType
}
}

我还实现了 KeyedDecodingContainer 扩展,用于将某些类型转换为另一种类型。 float 例如..

如何在解码时自动将枚举映射到我的模型,以便我获得附加到它的枚举值(参见枚举),而不是字符串?我可以直接使枚举符合 Codable 吗?

最佳答案

我对此的建议是继续并使 VehicleType 可解码。请注意,您可以使用 Decodable 而不是 Codable 来使事情变得更容易,并且如果您无论如何都不会将该对象转换回来,则不必实现编码逻辑。

你的最终代码看起来像这样:

enum DecodingError: Error {
case unknownVehiculeType
}

enum VehiculeType: Decodable {

case bus
case trolleybus
case tram

init(from decoder: Decoder) throws {
let rawValue = try decoder.singleValueContainer().decode(String.self)
switch rawValue {
case "AB", "ABA", "ABAA":
self = .bus
break
case "TBA", "TBAA":
self = .trolleybus
case "TW6", "TW7", "TW2":
self = .tram
default:
throw DecodingError.unknownVehiculeType
}
}
}

struct Departure: Decodable {

// let lineCode: String
let destination: String
let waitingTime: Int
let waitingMilliseconds: Int
let reroute: String
let vehiculeType: VehiculeType?


init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
destination = try container.decode(String.self, forKey: .destination)
waitingTime = try container.decode(Int.self, forKey: .waitingTime, transformFrom: String.self) ?? 0
waitingMilliseconds = try container.decode(Int.self, forKey: .waitingMilliseconds)
reroute = try container.decode(String.self, forKey: .reroute)
vehiculeType = try container.decodeIfPresent(VehiculeType.self, forKey: .vehiculeType)
}
}

extension Departure {
enum CodingKeys: String, CodingKey {
case destination = "destination"
case waitingTime = "attente"
case waitingMilliseconds = "attenteMilli"
case reroute = "deviation"
case vehiculeType = "vehiculeType"
}
}

关于swift - 使 Enum 符合 Codable 并映射到模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53090061/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com