问题描述
我正在使用 Urlsession 和 JSONDecoder 来获取和解码我的数据,但不知何故天气结构上的一些变量在我解码时返回 nil。可能跟utf8有关系,但是不知道怎么处理。
var request = URLRequest(url: URL(string: "myurlstring")
URLSession.shared.dataTask(with: request) {(data,resp,err) in
guard let data = data else { return }
do{
let decoder = JSONDecoder()
let weatherData = try decoder.decode(Weather.self,from: data)
completion(weatherData,nil)
} catch let err {
completion(nil,err)
}
}.resume()
我得到 nil 值,正如你在这里看到的:
但是在邮递员上我可以得到非零结果
这是我从邮递员那里得到的原始 json 数据
{"lat":39.92,"lon":32.85,"timezone":"Europe/Istanbul","timezone_offset":10800,"current":{"dt":1609623860,"sunrise":1609650620,"sunset":1609684530,"temp":2.26,"feels_like":-0.99,"pressure":1026,"humidity":72,"dew_point":-2,"uvi":0,"clouds":0,"visibility":10000,"wind_speed":1.37,"wind_deg":72,"weather":[{"id":800,"main":"Clear","description":"clear sky","icon":"01n"}]},"minutely":[{"dt":1609623900,"precipitation":0},{"dt":1609623960,{"dt":1609624020,{"dt":1609624080,{"dt":1609624140,{"dt":1609624200,{"dt":1609624260,{"dt":1609624320,{"dt":1609624380,{"dt":1609624440,{"dt":1609624500,{"dt":1609624560,{"dt":1609624620,{"dt":1609624680,{"dt":1609624740,{"dt":1609624800,{"dt":1609624860,{"dt":1609624920,{"dt":1609624980,{"dt":1609625040,{"dt":1609625100,{"dt":1609625160,{"dt":1609625220,{"dt":1609625280,{"dt":1609625340,{"dt":1609625400,{"dt":1609625460,{"dt":1609625520,{"dt":1609625580,{"dt":1609625640,{"dt":1609625700,{"dt":1609625760,{"dt":1609625820,{"dt":1609625880,{"dt":1609625940,{"dt":1609626000,{"dt":1609626060,{"dt":1609626120,{"dt":1609626180,{"dt":1609626240,{"dt":1609626300,{"dt":1609626360,{"dt":1609626420,{"dt":1609626480,{"dt":1609626540,{"dt":1609626600,{"dt":1609626660,{"dt":1609626720,{"dt":1609626780,{"dt":1609626840,{"dt":1609626900,{"dt":1609626960,{"dt":1609627020,{"dt":1609627080,{"dt":1609627140,{"dt":1609627200,{"dt":1609627260,{"dt":1609627320,{"dt":1609627380,{"dt":1609627440,{"dt":1609627500,"precipitation":0}],"hourly":[{"dt":1609621200,"icon":"01n"}],"pop":0},"temp":3.06,"feels_like":-0.06,"humidity":73,"dew_point":-1.15,"wind_speed":1.36,"wind_deg":73,{"dt":1609628400,"temp":3.55,"feels_like":0.24,"humidity":74,"dew_point":-0.57,"wind_speed":1.76,"wind_deg":69,{"dt":1609632000,"temp":3.7,"feels_like":0.26,"dew_point":-0.44,"wind_speed":1.98,"wind_deg":68,{"dt":1609635600,"temp":3.3,"feels_like":-0.02,"humidity":77,"dew_point":-0.3,"wind_speed":1.83,"wind_deg":66,{"dt":1609639200,"temp":3.08,"feels_like":-0.29,"humidity":78,"dew_point":-1.55,"wind_speed":1.9,{"dt":1609642800,"temp":2.68,"feels_like":-0.58,"humidity":80,"dew_point":-1.74,"clouds":2,"wind_speed":1.73,"wind_deg":61,{"dt":1609646400,"temp":2.91,"feels_like":-0.69,"dew_point":-1.77,"clouds":1,"wind_speed":2.19,"wind_deg":63,{"dt":1609650000,"temp":2.31,"feels_like":-0.83,"humidity":82,"dew_point":-1.84,"clouds":15,"wind_speed":1.56,"wind_deg":70,"weather":[{"id":801,"main":"Clouds","description":"few clouds","icon":"02n"}],{"dt":1609653600,"temp":3.76,"feels_like":0.53,"dew_point":-1.75,"uvi":0.2,"clouds":29,"wind_speed":1.69,"weather":[{"id":802,"description":"scattered clouds","icon":"03d"}],{"dt":1609657200,"temp":6.01,"feels_like":2.76,"humidity":63,"dew_point":-1.27,"uvi":0.55,"clouds":100,"wind_speed":1.7,"wind_deg":67,"weather":[{"id":804,"description":"overcast clouds","icon":"04d"}],{"dt":1609660800,"temp":7.55,"feels_like":4.39,"humidity":57,"dew_point":-1.22,"uvi":1.1,"wind_speed":1.59,"wind_deg":74,{"dt":1609664400,"temp":8.65,"feels_like":5.61,"pressure":1025,"humidity":53,"dew_point":-1,"uvi":1.56,"wind_speed":1.42,"wind_deg":79,{"dt":1609668000,"temp":9.47,"feels_like":6.36,"pressure":1024,"humidity":50,"dew_point":-0.8,"uvi":1.68,"wind_speed":1.52,"wind_deg":86,{"dt":1609671600,"temp":9.96,"feels_like":6.64,"pressure":1023,"humidity":49,"dew_point":-0.72,"uvi":1.41,"wind_speed":1.86,"wind_deg":84,{"dt":1609675200,"temp":10.16,"feels_like":7.02,"pressure":1022,"humidity":48,"dew_point":-0.76,"uvi":0.9,"wind_speed":1.58,{"dt":1609678800,"feels_like":7.12,"dew_point":-0.6,"uvi":0.39,"wind_speed":1.17,"wind_deg":76,{"dt":1609682400,"temp":8.25,"feels_like":5.56,"humidity":56,"dew_point":0.04,"wind_speed":1,"wind_deg":81,{"dt":1609686000,"temp":6.37,"feels_like":3.53,"dew_point":-0.42,"wind_speed":1.19,"wind_deg":85,"icon":"04n"}],{"dt":1609689600,"temp":6.08,"feels_like":3.17,"humidity":64,"dew_point":-0.51,"wind_speed":1.28,"wind_deg":80,{"dt":1609693200,"temp":5.66,"feels_like":2.84,"humidity":66,"wind_speed":1.15,"wind_deg":89,{"dt":1609696800,"temp":5.35,"feels_like":2.6,"humidity":67,"dew_point":-0.7,"wind_speed":1.03,"wind_deg":88,{"dt":1609700400,"temp":5.17,"feels_like":2.55,"humidity":68,"dew_point":-0.81,"wind_speed":0.86,"wind_deg":90,{"dt":1609704000,"temp":4.66,"feels_like":2.12,"humidity":70,"dew_point":-1.03,"wind_speed":0.72,{"dt":1609707600,"temp":4.11,"feels_like":1.43,"dew_point":-1.31,"wind_speed":0.89,"wind_deg":87,{"dt":1609711200,"temp":3.65,"feels_like":1.2,"humidity":75,"dew_point":-1.44,"wind_speed":0.59,"wind_deg":110,{"dt":1609714800,"temp":3.17,"feels_like":1,"wind_speed":0.17,"wind_deg":168,{"dt":1609718400,"temp":2.75,"feels_like":0.51,"clouds":98,"wind_speed":0.29,"wind_deg":36,{"dt":1609722000,"temp":2.38,"feels_like":-0.1,"dew_point":-1.33,"clouds":37,"wind_speed":0.63,"icon":"03n"}],{"dt":1609725600,"temp":2.1,"feels_like":-0.49,"humidity":83,"dew_point":-1.59,"clouds":19,"wind_speed":0.77,{"dt":1609729200,"temp":1.86,"feels_like":-0.54,"humidity":84,"clouds":12,"wind_speed":0.48,"wind_deg":93,{"dt":1609732800,"temp":1.65,"feels_like":-0.77,"humidity":85,"dew_point":-1.93,"clouds":9,"wind_speed":0.5,"wind_deg":108,{"dt":1609736400,"temp":1.62,"feels_like":-0.93,"humidity":86,"dew_point":-1.92,"clouds":8,"wind_speed":0.71,"wind_deg":125,{"dt":1609740000,"temp":3.22,"feels_like":0.54,"dew_point":-1.43,"clouds":6,"wind_speed":0.91,"wind_deg":114,"icon":"01d"}],{"dt":1609743600,"temp":6.78,"feels_like":4.06,"humidity":60,"dew_point":-1.04,"uvi":0.59,"wind_speed":0.96,"wind_deg":115,{"dt":1609747200,"temp":8.01,"feels_like":5.38,"dew_point":-0.88,"uvi":1.18,"wind_speed":0.87,"wind_deg":131,{"dt":1609750800,"temp":8.83,"feels_like":6.08,"pressure":1021,"dew_point":-0.47,"uvi":1.66,"wind_speed":1.05,"wind_deg":124,{"dt":1609754400,"temp":9.46,"feels_like":6.49,"pressure":1020,"humidity":51,"dew_point":0.02,"uvi":1.74,"clouds":47,"wind_deg":145,{"dt":1609758000,"temp":9.94,"feels_like":6.92,"pressure":1019,"humidity":52,"dew_point":0.55,"uvi":1.46,"clouds":58,"wind_deg":155,"weather":[{"id":803,"description":"broken clouds",{"dt":1609761600,"temp":10.25,"feels_like":7.2,"pressure":1018,"dew_point":0.95,"uvi":0.93,"clouds":65,"wind_deg":156,{"dt":1609765200,"temp":10.11,"humidity":54,"dew_point":1.3,"uvi":0.45,{"dt":1609768800,"temp":8.43,"feels_like":5.34,"humidity":62,"dew_point":1.79,"clouds":49,"wind_speed":1.92,"wind_deg":138,{"dt":1609772400,"temp":6.46,"feels_like":3.08,"dew_point":1.58,"clouds":32,"wind_speed":2.29,"wind_deg":116,{"dt":1609776000,"temp":5.78,"feels_like":2.68,"dew_point":1.65,"clouds":25,"wind_speed":1.93,"wind_deg":109,{"dt":1609779600,"temp":5.51,"feels_like":2.42,"humidity":76,"clouds":20,"wind_speed":1.94,"wind_deg":102,{"dt":1609783200,"temp":5.2,"feels_like":2.13,"dew_point":1.66,"clouds":17,"wind_speed":1.88,"wind_deg":98,{"dt":1609786800,"temp":4.96,"feels_like":1.8,"dew_point":1.41,"clouds":16,"wind_speed":1.96,{"dt":1609790400,"temp":4.73,"feels_like":1.54,"dew_point":1.14,"clouds":38,"wind_deg":99,"pop":0}],"daily":[{"dt":1609664400,"temp":{"day":8.65,"min":2.26,"max":10.16,"night":4.66,"eve":6.37,"morn":2.68},"feels_like":{"day":5.61,"night":2.12,"eve":3.53,"morn":-0.58},"pop":0,"uvi":1.68},"sunrise":1609737022,"sunset":1609770983,"temp":{"day":8.83,"min":1.62,"max":10.25,"night":4.73,"eve":6.46,"morn":1.86},"feels_like":{"day":6.08,"night":1.54,"eve":3.08,"morn":-0.54},"uvi":1.74},{"dt":1609837200,"sunrise":1609823421,"sunset":1609857436,"temp":{"day":6.76,"min":3.75,"max":6.93,"night":3.88,"eve":5.25,"morn":4.19},"feels_like":{"day":4.84,"night":1.69,"eve":2.76,"morn":0.95},"dew_point":1.73,"wind_speed":0.28,"weather":[{"id":501,"main":"Rain","description":"moderate rain","icon":"10d"}],"pop":0.84,"rain":3.33,"uvi":0.92},{"dt":1609923600,"sunrise":1609909818,"sunset":1609943891,"temp":{"day":7.92,"min":2.25,"max":9.68,"night":5.61,"eve":6.62,"morn":2.25},"night":3.19,"eve":3.93,"morn":-0.48},"dew_point":3.86,"wind_speed":1.35,"clouds":85,"uvi":1.53},{"dt":1610010000,"sunrise":1609996213,"sunset":1610030348,"temp":{"day":9.52,"min":3.3,"max":11.03,"night":5.83,"eve":7.61,"morn":3.3},"feels_like":{"day":6.47,"night":2.14,"eve":3.99,"morn":0.96},"humidity":59,"dew_point":2.02,"wind_deg":193,"clouds":73,"uvi":1.36},{"dt":1610096400,"sunrise":1610082606,"sunset":1610116805,"temp":{"day":8.46,"min":3.38,"max":10.97,"night":6.64,"eve":7.48,"morn":3.38},"feels_like":{"day":5.89,"night":4.27,"eve":5.51,"morn":0.69},"dew_point":0.64,"wind_speed":0.93,"wind_deg":186,"clouds":99,"uvi":2},{"dt":1610182800,"sunrise":1610168997,"sunset":1610203264,"temp":{"day":8.61,"min":7.2,"max":10.59,"night":9.15,"eve":10.06,"morn":7.46},"feels_like":{"day":5.94,"night":6.09,"eve":7.12,"morn":4.51},"dew_point":4.26,"wind_speed":2,"wind_deg":174,"weather":[{"id":500,"description":"light rain","pop":0.38,"rain":0.35,{"dt":1610269200,"sunrise":1610255385,"sunset":1610289724,"temp":{"day":12.17,"min":6.69,"max":14.54,"night":9.62,"eve":10.91,"morn":6.69},"feels_like":{"day":9.55,"night":7.19,"eve":8.57,"morn":3.86},"dew_point":4.61,"wind_deg":194,"clouds":74,"pop":0.51,"rain":0.99,"uvi":2}]}
ant 这是我的天气模型
struct Weather: Codable {
let lat,lon: Double
let timezone: String?
let timezoneOffset: Int?
let current: Current?
let minutely: [Minutely]?
let hourly: [Current]?
let daily: [Daily]?
enum CodingKeys: String,CodingKey {
case lat,lon,timezone
case timezoneOffset
case current,minutely,hourly,daily
}}
struct Current: Codable {
let dt,sunrise,sunset: Int?
let temp,feelsLike: Double?
let pressure,humidity: Int?
let dewPoint,uvi: Double?
let clouds,visibility: Int?
let windSpeed: Double?
let windDeg: Int?
let weather: [WeatherElement]?
let pop: Double?
let rain: Rain?
enum CodingKeys: String,CodingKey {
case dt,sunset,temp
case feelsLike
case pressure,humidity
case dewPoint
case uvi,clouds,visibility
case windSpeed
case windDeg
case weather,pop,rain
}}
struct Rain: Codable {
let the1H: Double?
enum CodingKeys: String,CodingKey {
case the1H
}}
struct WeatherElement: Codable {
let id: Int?
let main,weatherDescription,icon: String?
enum CodingKeys: String,CodingKey {
case id,main
case weatherDescription
case icon
}}
struct Daily: Codable {
let dt,sunset: Int?
let temp: Temp?
let feelsLike: FeelsLike?
let pressure,windSpeed: Double?
let windDeg: Int?
let weather: [WeatherElement]?
let clouds: Int?
let pop,uvi,rain: Double?
enum CodingKeys: String,humidity
case dewPoint
case windSpeed
case windDeg
case weather,rain
}}
struct FeelsLike: Codable {
let day,night,eve,morn: Double?}
struct Temp: Codable {
let day,min,max,night: Double?
let eve,morn: Double?}
struct Minutely: Codable {
let dt,precipitation: Double?}
这是我第一次来这里。抱歉我的错误。
解决方法
问题在于变量的名称,例如:
timezone_offset 应该完全一样,而不是 timezoneOffset
您可以更新枚举 CodingKeys 以避免错误的命名做法。
enum CodingKeys: String,CodingKey {
case lat,lon,timezone
case timezoneOffset = "timezone_offset"
case current,minutely,hourly,daily
}}