ios - 根据 SwiftUI 中的用户设置确定用户所需的温度单位 (F/C)
问题描述
有谁知道当前确定用户温度偏好设置状态的最佳方法?设置 -> 常规 -> 语言和区域 -> 温度单位下的那个?
我看过一些较旧的答案,但它们对我不起作用。我认为这种方法在过去几年中发生了变化。
解决方案
这适用于物理设备(它不适用于 iOS 模拟器):
let myFormatter = MeasurementFormatter()
let temperature = Measurement(value: 0, unit: UnitTemperature.celsius)
print(myFormatter.string(from: temperature))
如果设置 -> 常规 -> 语言和区域 -> 温度设置为华氏度,则输出 32°F 否则为 0°C。
如果您对状态感兴趣,您应该将返回的字符串解析为“°C”或“°F”
推荐阅读
- java - 如何让我的代码返回正确的变量?
- c# - 尝试在 ComboBox.Items.AddRange() 中显示文件名
- php - 如何将内部 SQLite 数据库中的数据与 MySQL 数据库同步(链接、匹配)?
- docker - 无法在 Gitlab-CI 中的 docker 容器之间进行通信
- c++ - 由 const 引用传递的回调函数调用错误
- css - 如何在水平行中显示 Plot.ly 模式栏图标
- php - wordpress 循环返回循环外的全部内容
- python-3.x - 使用请求的 Python SSL 证书错误
- tensorflow - TypeError:_variable_v2_call() 在张量流版本 = 2.0.0 中得到了一个意外的关键字参数“初始化器”
- bash - 复制从特定字符开始的每行的最后一个数字并在该字符之后追加