gpt4 book ai didi

arrays - 使用 Swift4 Codable 解码数组

转载 作者:行者123 更新时间:2023-12-05 00:14:54 27 4
gpt4 key购买 nike

我又在玩 Swift 4 Codable 了,就在我认为我掌握了它的窍门时,我在从响应中解码 Weather 键时遇到了这个问题。

 let jsonData = """
{"coord":{"lon":-113,"lat":35},
"weather":[{"id":800,"main":"Clear","description":"clear sky","icon":"01n"}],
"base":"stations",
"main":{"temp":291.15,"pressure":1022,"humidity":72,"temp_min":291.15,"temp_max":291.15},
"visibility":16093,"wind":{"speed":3.6,"deg":200},
"clouds":{"all":1},
"dt":1503294780,"sys":{"type":1,"id":321,"message":0.184,"country":"US","sunrise":1503320222,"sunset":1503367937},
"id":5308281,"name":"Paulden","cod":200}
"""

swift 模型:
//Top Level Container
struct Response: Codable {

enum ResponseKeys: String, CodingKey {
case name
case code = "cod"
case main
case weather
}
//Nested Level Keys: Response > Weather
enum WeatherKeys: String, CodingKey {
case weather
}
//Nested Level Keys: Response > Main
enum MainKeys: String, CodingKey {
case temp
case pressure
case humidity
case tempMin = "temp_min"
case tempMax = "temp_max"
}
//Properties
var name: String
var code: Int
var temp: Double
var pressure: Int
var weather:[WeatherObj]

//Custom Init
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: ResponseKeys.self)
let weatherContainer = try container.nestedContainer(keyedBy: WeatherKeys.self, forKey: .weather)
let mainContainer = try container.nestedContainer(keyedBy: MainKeys.self, forKey: .main)
self.name = try container.decode(String.self, forKey: .name)
self.code = try container.decode(Int.self, forKey: .code)
self.pressure = try mainContainer.decode(Int.self, forKey: .pressure)
self.temp = try mainContainer.decode(Double.self, forKey: .temp)

// Here is where it throws: the data couldn’t be read because it isn’t in the correct format
self.weather = try weatherContainer.decode([WeatherObj].self, forKey: .weather)
}
}

我不确定我做错了什么。其他一切都完美地解码到我的模型中。只有当我尝试解码数组时。有什么建议?

最佳答案

您解码了 weather键错误。它是顶级 key ,因此您无需创建子容器。这就够了:

self.weather = try container.decode([WeatherObj].self, forKey: .weather)

但是我实际上建议您创建一个 private struct这与 JSON 非常接近以简化解码过程。然后你可以挑选你想要初始化数据模型的部分:
struct Response: Codable {
var name: String
var code: Int
var temp: Double
var pressure: Int
var weather: [WeatherObj]

// This stays as close to the JSON as possible to minimize the amount of manual code
// It uses snake_case and the JSON's spelling of "cod" for "code". Since it's private,
// outside caller can never access it
private struct RawResponse: Codable {
var name: String
var cod: Int
var main: Main
var weather: [WeatherObj]

struct Main: Codable {
var temp: Double
var pressure: Int
}
}

init(from decoder: Decoder) throws {
let rawResponse = try RawResponse(from: decoder)

// Now pick the pieces you want
self.name = rawResponse.name
self.code = rawResponse.cod
self.temp = rawResponse.main.temp
self.pressure = rawResponse.main.pressure
self.weather = rawResponse.weather
}
}

关于arrays - 使用 Swift4 Codable 解码数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45795571/

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