成功地使Enum(具有关联类型)可编码

时间:2017-12-13 06:06:14

标签: swift codable

我无法正确解码我的Codable类型。我已经阅读了一些有关使用相关类型制作Codable枚举的教程。我搜索过拼写错误,姓名不匹配等等,但我无法发现任何错误。然而,每当我尝试对这些结构进行解码时(一个图层...这定义了一个"属性"在一个扩展中的一个层上......但是图层的所有其他预定义部分都是正确的en / de-编码),我找到了一个未找到的密钥" decode(from decoder:)方法中的异常。

extension Layer {

    struct Attribute: Codable {

        enum Value: Codable {

            case pulse(Double)
            case flash(Double)
            case draw(Double)

            private enum CodingKeys: String, CodingKey {
                case pulse, flash, draw
            }

            func encode(to encoder: Encoder) throws {

                var container = encoder.container(keyedBy: CodingKeys.self)

                switch self {
                case .pulse(let value):
                    try container.encode(value, forKey: .pulse)
                case .flash(let value):
                    try container.encode(value, forKey: .flash)
                case .draw(let value):
                    try container.encode(value, forKey: .draw)
                }
            }

            init(from decoder: Decoder) throws {

                let values = try decoder.container(keyedBy: CodingKeys.self)

                do {
                    let value = try values.decode(Double.self, forKey: .pulse)
                    self = .pulse(value)
                } catch (let error) {
                    print(error)
                }

                do {
                    let value = try values.decode(Double.self, forKey: .draw)
                    self = .draw(value)
                } catch (let error) {
                    print(error)
                }

                do {
                    let value = try values.decode(Double.self, forKey: .flash)
                    self = .flash(value)
                } catch (let error) {
                    print(error)
                }

                self = .draw(0.0)
            }


        }

        var value: Value

        init(value: Value) {
            self.value = value
        }
    }
}

1 个答案:

答案 0 :(得分:2)

我认为在解码之前,你应首先检查解码器容器中是否存在密钥。目前,您正在这样做:

do {
    let value = try values.decode(Double.self, forKey: .pulse)
    self = .pulse(value)
} catch (let error) {
    print(error)
}

do {
    let value = try values.decode(Double.self, forKey: .draw)
    self = .draw(value)
} catch (let error) {
    print(error)
}

do {
    let value = try values.decode(Double.self, forKey: .flash)
    self = .flash(value)
} catch (let error) {
    print(error)
}

解码器容器无法在那里拥有所有三个键,不是吗?

所以,请在解码前检查:

if values.contains(.pulse) {
    do {
        let value = try values.decode(Double.self, forKey: .pulse)
        self = .pulse(value)
        return // remember to return here, so you don't set self back to .draw(0.0) again!
    } catch (let error) {
        print(error)
    }
} else if values.contains(.draw) {
    do {
        let value = try values.decode(Double.self, forKey: .draw)
        self = .draw(value)
        return
    } catch (let error) {
        print(error)
    }
} else if values.contains(.flash) {
    do {
        let value = try values.decode(Double.self, forKey: .flash)
        self = .flash(value)
        return
    } catch (let error) {
        print(error)
    }
}