【问题标题】:Convert Kelvin into Celsius in Swift在 Swift 中将开尔文转换为摄氏度
【发布时间】:2016-11-07 21:47:01
【问题描述】:

我正在尝试从用户当前位置检索温度。

我正在使用 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 json swift double openweathermap


【解决方案1】:
if let kelvinTemp = main["temp"] as? Double {
    let celsiusTemp = kelvinTemp - 273.15
    self.tempLabel.text = String(format: "%.0f", celsiusTemp)
}

或者干脆

self.tempLabel.text = String(format: "%.0f", temperature - 273.15)

【讨论】:

    【解决方案2】:

    对于 Swift 4.2

    使用测量格式化程序。

    let mf = MeasurementFormatter()
    

    此方法将一种温度类型(开尔文、摄氏、华氏)转换为另一种:

      func convertTemp(temp: Double, from inputTempType: UnitTemperature, to outputTempType: UnitTemperature) -> String {
        mf.numberFormatter.maximumFractionDigits = 0
        mf.unitOptions = .providedUnit
        let input = Measurement(value: temp, unit: inputTempType)
        let output = input.converted(to: outputTempType)
        return mf.string(from: output)
      } 
    

    用法:

    let temperature = 291.0
    let celsius = convertTemp(temp: temperature, from: .kelvin, to: .celsius) // 18°C 
    let fahrenheit = convertTemp(temp: temperature, from: .kelvin, to: .fahrenheit) // 64°F
    

    要输出局部温度格式,请删除mf.unitOptions = .providedUnit这一行

    【讨论】:

    • 我会说这是最好的答案,因为它使用了本机库。谢谢@Marcy!
    【解决方案3】:

    根据上面的代码,在我看来正确的地方是在你得到温度之后

    if let temperatureInKelvin = main["temp"] as? Double {
      let temperatureInCelsius = temperatureInKelvin - 273.15
      self.tempLabel.text = String(format: "%.0f", temperature)
    }
    

    不过,在未来,我可能会在一个单独的类中解析您的 JSON 值,并将它们存储在一个模型对象中,以便您稍后调用。

    【讨论】:

      【解决方案4】:

      上述方便函数(针对 Swift 5.3 更新)的更简单示例如下:

      func convertTemperature(temp: Double, from inputTempType: UnitTemperature, to outputTempType: UnitTemperature) -> Double {
          let input = Measurement(value: temp, unit: inputTempType)
          let output = input.converted(to: outputTempType)
          return output.value
      }
      

      【讨论】:

        【解决方案5】:

        这里:

        self.tempLabel.text = String(format: "%.0f", temperature - 273.15)
        

        或者你可以在这里做(伪语法,因为我不太了解 Swift):

        if let temperature = (main["temp"] as? Double) - 273.15 {
            self.tempLabel.text = String(format: "%.0f", temperature)
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2023-02-24
          • 2015-04-14
          • 2021-11-13
          • 2022-12-04
          • 1970-01-01
          • 1970-01-01
          • 2016-02-08
          • 2018-08-10
          相关资源
          最近更新 更多