我試圖從用戶當前位置檢索溫度。將開爾文轉換爲Swift中的攝氏溫度
我使用OpenWeatherMap中的API。問題是,它們提供的溫度默認爲開爾文,我希望以攝氏度計。
我知道我只需要從開爾文值中減去273.15 ....?但我正在努力弄清楚在哪裏做到這一點。
我的設置我的標籤代碼:
var jsonData: AnyObject?
func setLabels(weatherData: NSData) {
do {
self.jsonData = try NSJSONSerialization.JSONObjectWithData(weatherData, options: []) as! NSDictionary
} catch {
//handle error here
}
if let name = jsonData!["name"] as? String {
locationLabel.text = "using your current location, \(name)"
}
if let main = jsonData!["main"] as? NSDictionary {
if let temperature = main["temp"] as? Double {
self.tempLabel.text = String(format: "%.0f", temperature)
}
}
}
誰能幫我得到這個權利,請,因爲我真的不知道從哪裏開始,謝謝。
讓我知道你是否需要查看我的更多代碼。
僅供參考 - 隨着iOS的10/MacOS的塞拉利昂,蘋果已將基準測量和單位API引入,以處理您的轉換以及本地化。鏈接到文檔:https://developer.apple.com/reference/foundation/nsmeasurement以及免費的WWDC視頻:https://developer.apple.com/videos/play/wwdc2016/238/ –