【问题标题】:Swift 4 encode nested enum returns emptySwift 4 编码嵌套枚举返回空
【发布时间】:2018-11-17 09:47:27
【问题描述】:

我正在使用一个 API,它的属性可以有不同的类型

属性可以是 Ids 或 Objects

我想构建一个通用类型,用 swift Codables 为我处理这个问题

例子:

"platforms": [
    6
]

"platforms": [
    {
        "id": 6,
        "name": "PC (Microsoft Windows)",
        "slug": "win",
        "url": "https://www.igdb.com/platforms/win",
        "created_at": 1297639288000,
        "updated_at": 1470063140518,
        "website": "http://windows.microsoft.com/",
        "alternative_name": "mswin"
    }
]

我创建了一个名为 ObjectType 的间接枚举来处理这个

extension ObjectType{
    enum CodingError: Error {
        case decoding(String)
    }
    enum CodableKeys: String, CodingKey {
        case Struct, Id
    }
}

/**
    ObjectType keeps track of struct expansion with two different 'states'
    Struct OR Int64
    If the request is expanded then the ObjectType will be a struct of that type. Otherwise it will be the id

    @param T the struct type it should expand to, if expanded.
*/
public indirect enum ObjectType<T: Codable>: Codable {
    case Struct(T)
    case Id(Int64)

    // decodes the response to the correct 'state', Struct or Int64.
    public init(from decoder: Decoder) throws {
        let values = try decoder.singleValueContainer()

        if let standardID = try? values.decode(Int64.self) {
            self = .Id(standardID)
        } else if let extendedID = try? values.decode(T.self) {
            self = .Struct(extendedID)
        } else {
            throw CodingError.decoding("Decoding Failed \(dump(values))")
        }
    }

    // encodes the response to the correct 'state', Struct or Int64.
    public func encode(to encoder: Encoder) throws {
        var container = encoder.container(keyedBy: CodableKeys.self)

        switch self {
        case let .Struct(extendedID):
            /* this line is weird */
            try container.encode(extendedID, forKey: .Struct)

        case let .Id(standardID):
            try container.encode(standardID, forKey: .Id)
        }
    }

这适用于解码,但不适用于编码结构。

在 Xcode 中为 .Struct 案例调试“try container.encode(extendedID, forKey: .Struct)”行返回“(())”,空结构。

我不明白为什么编码器在这里返回空,我做错了什么?

【问题讨论】:

    标签: swift xcode swift4 codable


    【解决方案1】:

    所以在尝试了一堆不同的事情之后,我设法解决了它并得到了我想要的结果。

    我没有正确使用编码器。 这是我的旧代码:

    public func encode(to encoder: Encoder) throws {
        var container = encoder.container(keyedBy: CodableKeys.self)
    
        switch self {
        case let .Struct(extendedID):
            /* this line is weird */
            try container.encode(extendedID, forKey: .Struct)
    
        case let .Id(standardID):
            try container.encode(standardID, forKey: .Id)
        }
    }
    

    这是工作代码:

    public func encode(to encoder: Encoder) throws {
    /* var container = encoder.container(keyedBy: CodableKeys.self) */
    
        switch self {
        case let .Struct(extendedID):
            try extendedID.encode(to: encoder)
         /* try container.encode(extendedID, forKey: .Struct) */
    
        case let .Id(standardID):
            try standardID.encode(to: encoder)
         /* try container.encode(standardID, forKey: .Id) */
        }
    }
    

    该解决方案看起来非常相似,但我仍然不明白为什么它有效,而不是我以前的解决方案。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-16
      • 1970-01-01
      相关资源
      最近更新 更多