可选键的默认枚举值



我有一个总是有text的JSON对象,有时有一个format键,当它没有时,我想对我的SubStep.Format枚举使用.regular大小写。

我知道我需要覆盖SubStep中的init,但是我该怎么做才能检测到格式键,如果为零,请使用.regular,然后解码始终存在的text键?

谢谢

struct SubStep: Decodable {
    enum Format: String, Decodable {
        case bold = "bold"
        case regular = "regular"
        init(from decoder: Decoder) {
            do {
                let label = try decoder.singleValueContainer().decode(String.self)
                self = Format(rawValue: label) ?? .regular
            } catch {
                self = .regular
            }
        }
    }
    let format: SubStep.Format
    let text: String
    init(from decoder: Decoder) throws {
       // something in here
    }
}

不需要为Format创建自定义init(from:)方法,你只需要一个用于SubStep的方法。您需要使用 container.decodeIfPresent(_:forKey:) 来解码 JSON 中可能不存在的密钥,在这种情况下,它会返回 nil

struct SubStep: Decodable {
    enum Format: String, Decodable {
        case bold
        case regular
    }
    let format: SubStep.Format
    let text: String
    private enum CodingKeys: String, CodingKey {
        case text, format
    }
    init(from decoder: Decoder) throws {
        let container = try decoder.container(keyedBy: CodingKeys.self)
        self.text = try container.decode(String.self, forKey: .text)
        self.format = try container.decodeIfPresent(Format.self, forKey: .format) ?? .regular
    }
}

与您的问题无关,但您无需为enum事例提供String rawValue,如果它们的 rawValue 与事例名称完全匹配,编译器将为您自动合成这些事例。

作为替代解决方案,您不必在分析期间插入默认值 - 您可以改用计算属性。

struct SubStep: Decodable {
    enum Format: String, Decodable {
        case bold
        case regular
    }
    private let formatOptional: Format?
    let text: String
    private enum CodingKeys: String, CodingKey {
        case formatOptional = "format"
        case text
    }
    var format: Format {
        return formatOptional ?? .regular
    }
}

最新更新