使枚举与Codable相符并映射至模型

时间:2018-10-31 18:43:16

标签: swift enums codable

我正在访问公共交通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扩展名,用于将某些类型转换为另一种类型。例如浮动。

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

1 个答案:

答案 0 :(得分:1)

我对此的建议是继续进行,使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"
    }
}