在从OpenWeatherMap API实现Rain时,我遇到了以下错误:
“h”不是整数文本中的有效数字
我有一个单独的.swift文档,它解析了所有的API引用,并尝试将Rain解析为:
struct Rain: Decodable {
let 3h: Double?
}
但我不知道如何处理“Rain:3h”,这样就不会发生错误。?
有人可以看看OpenWeatherMap API并告诉我他们的想法吗?
更新:
结构斯威夫特:
struct Rain: Decodable {
enum CodingKeys: String, CodingKey { case threeHours = "3h" }
let threeHours: Double?
}
视图控制器.swift
@IBOutlet weak var precipitation: UILabel!
let clouding = (self.rain?.threeHours!)!
precipitation.text = precipitation.text! + " " + String(format:"%.0f", clouding)
最佳答案
只需通过编码键翻译密钥
struct Rain: Decodable {
enum CodingKeys: String, CodingKey { case threeHours = "3h" }
let threeHours: Double?
}
或
struct Rain: Codable {
enum CodingKeys: String, CodingKey { case h3 = "3h" }
let h3: Double?
}