首页 > 解决方案 > Swift:在for循环中从JSON打印具有不同类型(枚举)的变量

问题描述

情况是这样的: 如何解码不同类型的 JSON 属性?

我使用了在链接问题中标记为解决方案的代码,我想在 for 循环中打印一个变量:

struct GetEvents: Decodable{
    var id: String?
    var expansion: String?
    var distance: Distance?
}

enum Distance: Codable {
    case int(Int)
    case string(String)

    func encode(to encoder: Encoder) throws {
        var container = encoder.singleValueContainer()
        switch self {
        case .int(let v): try container.encode(v)
        case .string(let v): try container.encode(v)
        }
    }

    init(from decoder: Decoder) throws {
        let value = try decoder.singleValueContainer()
        do {
            self = .int(try value.decode(Int.self))
        } catch DecodingError.typeMismatch {
            self = .string(try value.decode(String.self))
        }
    }

    enum ParseError: Error {
        case notRecognizedType(Any)
    }
}

我尝试使用 for 循环打印所有变量,因为我在对象中有多个对象。

for i in 0...(getEventsText.items.count - 1) {
                    if let idAsString = getEventsText.items[i].id {
                        print(idAsString)
                    }
                    if let distanceAsString = getEventsText.items[i].distance {
                        print(distanceAsString)
                    }
                    if let epansionAsString = getEventsText.items[i].offer_expansion {
                        print(expansionAsString)
                    }

我得到了 id 和扩展,但距离显示为 int(-1) 而不是 -1

如果我像这样输出它:

for x in range getEventsText.items {
    print(x)
}

它回馈

GetEvents(id:可选(“7576”),距离:Swift.ImplicitlyUnwrappedOptional.some(DB_Read_enums_Test1.Distance.int(0)),扩展:可选(“0”))

标签: jsonswiftcodable

解决方案


让我们Distance实现CustomStringConvertible并添加以下内容

var description: String {
    switch self {
        case let .int(value):
            return "\(value)"
        case let .string(value):            
            return value
    }
}

enum Distance: Codable, CustomStringConvertible {
    case int(Int)
    case string(String)

    var description: String {
        switch self {
        case let .int(value):
            return "\(value)"
        case let .string(value):            
            return value
    }

    //rest of code
}

测试用例

let data = """
    {"id": "7576", "expansion": "0", "distance": -1}
    """.data(using: .utf8)!

do {
    let decoder = JSONDecoder()

    let result = try decoder.decode(GetEvents.self, from: data)
    print(String(describing: result.id))
    print(String(describing: result.expansion))
    print(String(describing: result.distance))    
} catch {
    print(error)
}

输出是

Optional("7576")
Optional("0")
Optional(-1)

如果我将我的 json 更改为 "..."distance": "long"} 然后最后一个输出行更改为

Optional(long)

推荐阅读