【问题标题】:Server communication error handling in swiftswift中的服务器通信错误处理
【发布时间】:2019-11-02 18:31:00
【问题描述】:

在服务器通信中,如果我们不知道传入的是什么类型的数据,我们如何在 Swift 4 及更高版本中预先定义它是(字符串还是整数)?

即如何假设传入的数据是字符串还是整数以及如何处理动态服务器数据的错误?

【问题讨论】:

  • 请用一个示例 json 阐明问题,如果可能的话,您到目前为止尝试了什么。

标签: swift4 swift5


【解决方案1】:

{"response":{"id":"353","specification":[{"item_value":3,"item_name":"Mileage"},{"item_value":"二手车","item_name ":"Condition"},{"item_value":"BMW","item_name":"Make"},{"item_value":"7-Series","item_name":"Model"},{"item_value": 2019,"item_name":"年份"},{"item_value":"Both","item_name":"首选销售渠道"},{"item_value":"asd","item_name":"出口位置"}] ,"description":"asd","lat_location":null,"isFavorite":false,"user_id":"156","title":"asd","seller_mobile":"971551909257","price":334 ,"location":"asd","date":"04 Nov 2019","long_location":null,"images":[],"result":1,"seller_id":78}}

查看上面 json 的模态类,在上面的 json 中,规范数组包含整数和字符串值,我们可以很容易地从中获取值。

struct GetResponse_DetailPage: Codable {
    let response: Response_DetailPage?
}



struct Response_DetailPage: Codable {
    let result: Int?
    let id: String?
    let sellerID: Int?
    let sellerMobile: String?
    let specification: [Specification_DetailPage]?
    let images: [Image_DetailPage]?
    let price: Int?
    let location, responseDescription, date, latLocation: String?
    let longLocation, title: String?
    let isFavorite: Bool?
    let userID: String?

    enum CodingKeys: String, CodingKey {
        case result, id
        case sellerID = "seller_id"
        case sellerMobile = "seller_mobile"
        case specification, images, price, location
        case responseDescription = "description"
        case date
        case latLocation = "lat_location"
        case longLocation = "long_location"
        case title, isFavorite
        case userID = "user_id"
    }
}
struct Image_DetailPage: Codable {
    let adImg: String?

    enum CodingKeys: String, CodingKey {
        case adImg = "ad_img"
    }
}
struct Specification_DetailPage: Codable {
    let itemName : String?
    let itemValue: ItemValue_DetailPage?

    enum CodingKeys: String, CodingKey {
        case itemName = "item_name"
        case itemValue = "item_value"
    }
}
enum ItemValue_DetailPage: Codable {
    case intege(String)
    case string(String)

    init(from decoder: Decoder) throws {
        let container = try decoder.singleValueContainer()
        if let x = try? container.decode(Int.self) {
            self = .intege(String(describing: x))
            return
        }
        if let x = try? container.decode(String.self) {
            self = .string(x)
            return
        }
        throw DecodingError.typeMismatch(ItemValue_DetailPage.self, DecodingError.Context(codingPath: decoder.codingPath, debugDescription: "Wrong type for ItemValue"))
    }

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

检查一下,如果你有问题,然后评论它

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-27
    • 2015-02-08
    • 2018-08-06
    • 2011-07-24
    • 1970-01-01
    相关资源
    最近更新 更多