nas*_*avi 3 generics json decode response swift
如何在 swift 中将 json 解码为通用模型?
在java中解码json我使用GSON,一般来说我使用并不重要。在<T<E>> or ArrayList<E>swift中数组是一个结构体并且不能继承并且它没有实现Decodable。
我正在寻找一个通用的优雅类来在我的所有网络服务中使用。
我的场景:
我有 json 响应
{
"status": true,
"message": "",
"code": 200,
"response": [{
"id": 43
}]
}
Run Code Online (Sandbox Code Playgroud)
以及来自 Web 服务的通用响应模型:
class GeneralResponse< T : Decodable >:NSObject,Decodable{
var status = false
var message = ""
var code = -1
var response : T?
private enum CodingKeys: String, CodingKey {
case status
case message
case code
case response
}
required public init(from decoder: Decoder) throws{
let container = try decoder.container(keyedBy: CodingKeys.self)
status = try container.decode(Bool.self, forKey: .status)
message = try container.decode(String.self, forKey: .message)
code = try container.decode(Int.self, forKey: .code)
response = try container.decode(T.self, forKey: .response)
}
}
class ItemDemoModel:Decodable {
var id = -1
private enum ItemDemModelCodingKeys : String, CodingKey {
case id
}
required init(from decoder:Decoder) throws {
let container = try decoder.container(keyedBy: ItemDemModelCodingKeys.self)
id = try container.decode(Int.self, forKey: .id)
}
}
Run Code Online (Sandbox Code Playgroud)
响应变量可以是 ItemDemoModel 或 ItemDemoModel 数组。
例如:
它可以是GeneralResponse<Array<ItemDemoModel>>>
或GeneralResponse<ItemDemoModel>>
谢谢。
如果您声明一个Decodable与键同名的属性,那么json您实际上不需要enum定义Coding键和初始化程序来手动将每个属性与键映射。
此外,除非您有特定的用例,否则无需从NSObjectin继承。Swift看看声明,似乎没有必要,所以你GeneralResponse可以像这样简单地重新声明,
class GeneralResponse<T: Decodable>: Decodable {
var code: Int
var status: Bool
var message: String?
var response : T?
}
Run Code Online (Sandbox Code Playgroud)
同样,ItemDemoModel可以声明为这样,
class ItemDemoModel: Decodable {
var id: Int
}
Run Code Online (Sandbox Code Playgroud)
现在您可以如下设置您的服务来获取GeneralResponse<T>任何请求,
struct RequestObject {
var method: String
var path: String
var params: [String: Any]
}
class WebService {
private let decoder: JSONDecoder
public init(_ decoder: JSONDecoder = JSONDecoder()) {
self.decoder = decoder
}
public func decoded<T: Decodable>(_ objectType: T.Type,
with request: RequestObject,
completion: @escaping (GeneralResponse<T>?, Error?) -> Void) {
// Here you should get data from the network call.
// For compilation, we can create an empty object.
let data = Data()
// Now parsing
do {
let response = try self.decoder.decode(GeneralResponse<T>.self, from: data)
completion(response, nil)
} catch {
completion(nil, error)
}
}
}
Run Code Online (Sandbox Code Playgroud)
用法
let request = RequestObject(method: "GET", path: "https://url.com", params: [:])
WebService().decoded([ItemDemoModel].self, with: request) { (response, error) in
if let items = response?.response {
print(items)
}
}
Run Code Online (Sandbox Code Playgroud)
附注;您必须用于声明数组和字典,如下所示,
let array: Array<SomeType>
let dictionary: Dictionary<String: SomeType>
let arrayOfDictionary: Array<Dictionary<String: SomeType>>
Run Code Online (Sandbox Code Playgroud)
但是通过Swift的类型推断,您可以像下面这样简单地声明 anarray和 a ,dictionary
let array: [SomeType]
let dictionary: [String: SomeType]
let arrayOfDictionary: [[String: SomeType]]
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7076 次 |
| 最近记录: |