gpt4 book ai didi

swift - 如何以通用格式编写可编码

转载 作者:行者123 更新时间:2023-12-02 02:40:02 25 4
gpt4 key购买 nike

我已经了解如何为服务响应结构制作可编码的包装类。但有时在服务器端属性值会有所不同,可能是 Int 或 String。

示例

struct ResponseDataModel : Codable{
let data : DataClass?
enum CodingKey: String, CodingKey{
case data = "data"

}
init(from decoder: Decoder) throw {
let values = try decoder.container(keyedBy: CodingKeys.self)
data = try values.decodeIfPresent(DataClass.self, forKey:.data)
}
}

struct DataClass : Codable{
let id : Int
let name : String?
let age : Int?

enum CodingKey: String, CodingKey{
case id = "id"
case name = "name"
case age = "age"

}
init(from decoder: Decoder) throw {
let values = try decoder.container(keyedBy: CodingKeys.self)
id = try values.decodeIfPresent(Int.self, forKey:.it)
name = try values.decodeIfPresent(String.self, forKey:.name)
age = try values.decodeIfPresent(Int.self, forKey:.age)
}
}

我想使用通用方式,如果 id int string 无论它是什么,它都应该使用 id 值数据绑定(bind)到我的 Controller 。

let id : <T>

如何以通用格式编写可编码代码。

最佳答案

您可以使用以下模型来做到这一点:

struct ResponseDataModel<T: Codable>: Codable{
let data : DataClass<T>?
enum CodingKeys: String, CodingKey{
case data = "data"

}
init(from decoder: Decoder) throws {
let values = try decoder.container(keyedBy: CodingKeys.self)
data = try values.decodeIfPresent(DataClass<T>.self, forKey:.data)
}
}

struct DataClass<T: Codable>: Codable {
let id: T?
let name: String?
let age: Int?

enum CodingKeys: String, CodingKey{
case id = "id"
case name = "name"
case age = "age"

}
init(from decoder: Decoder) throws {
let values = try decoder.container(keyedBy: CodingKeys.self)
id = try values.decodeIfPresent(T.self, forKey:.id)
name = try values.decodeIfPresent(String.self, forKey:.name)
age = try values.decodeIfPresent(Int.self, forKey:.age)
}
}

但是,当您调用 JSONDecoderdecode(_:from:) 函数时,您应该始终知道 id 属性的类型,例如这个:

let decoder = JSONDecoder()

do {
let decoded = try decoder.decode(ResponseDataModel<Int>.self, from: data)
print(decoded)
} catch {
print(error)
}

或者,您可以使用以下模型始终将 id 映射为 Int,即使您的服务器将其作为 String 发送:

struct ResponseDataModel: Codable{
let data : DataClass?
enum CodingKeys: String, CodingKey{
case data = "data"

}
init(from decoder: Decoder) throws {
let values = try decoder.container(keyedBy: CodingKeys.self)
data = try values.decodeIfPresent(DataClass.self, forKey:.data)
}
}

struct DataClass: Codable {
let id: Int?
let name: String?
let age: Int?

enum CodingKeys: String, CodingKey{
case id = "id"
case name = "name"
case age = "age"

}
init(from decoder: Decoder) throws {
let values = try decoder.container(keyedBy: CodingKeys.self)
do {
id = try values.decodeIfPresent(Int.self, forKey:.id)
} catch DecodingError.typeMismatch {
if let idString = try values.decodeIfPresent(String.self, forKey:.id) {
id = Int(idString)
} else {
id = nil
}
}
name = try values.decodeIfPresent(String.self, forKey:.name)
age = try values.decodeIfPresent(Int.self, forKey:.age)
}
}

关于swift - 如何以通用格式编写可编码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63773962/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com