gpt4 book ai didi

swift - 如何从 OpenWeatherMap 解析 "Rain"?

转载 作者:行者123 更新时间:2023-11-28 14:09:47 25 4
gpt4 key购买 nike

当从 OpenWeatherMap API 实现 Rain 时,我遇到了这个错误:

'h' 不是整数文字中的有效数字

我有一个单独的 .swift 文档来解析所有 API 引用,并尝试像这样解析 Rain:

struct Rain: Decodable {
let 3h: Double?
}

但我不确定如何解决“Rain: 3h”这样错误就不会发生。?

有人可以看看 OpenWeatherMap API 并告诉我他们的想法吗?

更新:

结构.swift:

struct Rain: Decodable {
enum CodingKeys: String, CodingKey { case threeHours = "3h" }

let threeHours: Double?
}

ViewController.swift

@IBOutlet weak var precipitation: UILabel!


let clouding = (self.rain?.threeHours!)!
precipitation.text = precipitation.text! + " " + String(format:"%.0f", clouding)

最佳答案

只需通过 CodingKeys 翻译 key

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?
}

关于swift - 如何从 OpenWeatherMap 解析 "Rain"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52683413/

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