【发布时间】:2018-10-06 21:17:18
【问题描述】:
当从 OpenWeatherMap API 实现 Rain 时,我遇到了这个错误:
'h' 不是整数文字中的有效数字
我有一个单独的 .swift 文档来解析所有 API 引用,并尝试将 Rain 解析为:
struct Rain: Decodable {
let 3h: Double?
}
但我不确定如何解决“Rain: 3h”这样错误不会发生。?
有人可以看看 OpenWeatherMap API 并告诉我他们的想法吗?
更新:
struct.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,如Ultimate Guide to JSON Parsing with Swift 4中所述
标签: swift xcode openweathermap