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

fquxozlt  于 2023-03-22  发布在  Swift
关注(0)|答案(1)|浏览(115)

我有一个JSON,它有两个不同类型的变量(与here相同)。
我使用了链接问题中标记为解决方案的代码,我想在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"), distance: Swift.ImplicitlyUnwrappedOptional<DB_Read_enums_Test1.Distance>.some(DB_Read_enums_Test1.Distance.int(0)), expansion: Optional("0"))
jchrr9hc

jchrr9hc1#

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)

相关问题