Swift:在for循环中从JSON打印具有不同类型(枚举)的变量

时间:2019-03-08 11:37:39

标签: json swift codable

情况是这样的: How to decode a JSON property with different types?

我在链接的问题中使用了标记为解决方案的代码,我想在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:Optional(“ 7576”),距离:Swift.ImplicitlyUnwrappedOptional.some(DB_Read_enums_Test1.Distance.int(0)),扩展:Optional(“ 0”))

1 个答案:

答案 0 :(得分:1)

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)