使枚举符合可编码,并映射到模型



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

它似乎主要起作用,但API没有正确规范化,这意味着我得到了相同属性的不同类型的数据(总线可以是Int或String等…(

我正在尝试将vehicule类型的属性映射到一个枚举,它看起来像这样:

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扩展,用于将某些类型转换为另一种类型。例如浮动。。

当枚举被解码时,我如何自动将其映射到我的模型,以便获得附加到它的枚举值(请参见枚举(,而不是字符串?我可以直接使enum符合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"
}
}

相关内容

  • 没有找到相关文章

最新更新