我如何知道 iOS 上的默认度量系统(英制或公制)?

发布于 2024-12-04 18:05:50 字数 86 浏览 2 评论 0原文

我如何知道 iOS 上的默认度量系统(英制或公制)?

如何从设备设置中获取此首选项,以便我知道在我的应用程序中显示什么?

谢谢

How do I know which is the default measure system (imperial or metric) on iOS ?

How do I get this preference from the device settings, so I know what to display in my app ?

thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

不忘初心 2024-12-11 18:05:50

NSLocale 可以告诉你:

NSLocale *locale = [NSLocale currentLocale]; 
BOOL isMetric = [[locale objectForKey:NSLocaleUsesMetricSystem] boolValue];

只有三个国家不使用公制:美国、利比里亚和缅甸。后者使用自己的系统,前两者使用英制单位。

苹果文档说(强调我的):

NSLocaleUsesMetricSystem

指示区域设置是否使用公制的标志键。
对应的值是一个 Boolean NSNumber 对象。 如果该值为“否”,您通常可以采用美国测量单位(例如法定英里)。

适用于 iOS 2.0 及更高版本。

The NSLocale can tell you:

NSLocale *locale = [NSLocale currentLocale]; 
BOOL isMetric = [[locale objectForKey:NSLocaleUsesMetricSystem] boolValue];

Only three countries do not use the metric system: the US, Liberia and Myanmar. The later uses its own system, the former two use Imperial Units.

Apples documentation says (emphasis mine):

NSLocaleUsesMetricSystem

The key for the flag that indicates whether the locale uses the metric system.
The corresponding value is a Boolean NSNumber object. If the value is NO, you can typically assume American measurement units (for example, the statute mile).

Available in iOS 2.0 and later.

零崎曲识 2024-12-11 18:05:50

@DarkDust 对 swift3 的回答

//User region setting return
let locale = Locale.current //NSLocale.current

//Returns true if the locale uses the metric system 
let isMetric = locale.usesMetricSystem

@DarkDust answer for swift3

//User region setting return
let locale = Locale.current //NSLocale.current

//Returns true if the locale uses the metric system 
let isMetric = locale.usesMetricSystem
花之痕靓丽 2024-12-11 18:05:50

这是一个快速版本

var locale = NSLocale.currentLocale()
let isMetric = locale.objectForKey(NSLocaleUsesMetricSystem) as! Bool

here's a swift version

var locale = NSLocale.currentLocale()
let isMetric = locale.objectForKey(NSLocaleUsesMetricSystem) as! Bool
一曲琵琶半遮面シ 2024-12-11 18:05:50

对于迅捷3

    let locale = NSLocale.current
    let isMetric = locale.usesMetricSystem

For swift 3

    let locale = NSLocale.current
    let isMetric = locale.usesMetricSystem
小傻瓜 2024-12-11 18:05:50

正如其他人之前提到的,英国混合使用公制和英制单位。

我建议使用 iOS 10 中引入的新 MeassurementFormatter 来处理大多数这些差异:

import Foundation

let locale = Locale(identifier: "EN_UK")
locale.usesMetricSystem // true!
var formatter = MeasurementFormatter()
formatter.locale = locale
formatter.string(from: Measurement(value: 1000, unit: UnitLength.meters)) // 0.621 mi

要将距离呈现为本地自然单位中的字符串,请使用:

let distanceInMeters: CLLocationDistance = 1000
let formatter = MeasurementFormatter()
formatter.string(from: Measurement(value: distanceInMeters, unit: UnitLength.meters)) // 0.621 mi

官方文档:https://developer.apple.com/documentation/foundation/measurementformatter

As others mentioned before, the UK uses a mix of metric and imperial units.

I would recommend using the new MeassurementFormatter introduced in iOS 10 which handles most of these discrepancies:

import Foundation

let locale = Locale(identifier: "EN_UK")
locale.usesMetricSystem // true!
var formatter = MeasurementFormatter()
formatter.locale = locale
formatter.string(from: Measurement(value: 1000, unit: UnitLength.meters)) // 0.621 mi

To render a distance as a string in local, natural unit, use:

let distanceInMeters: CLLocationDistance = 1000
let formatter = MeasurementFormatter()
formatter.string(from: Measurement(value: distanceInMeters, unit: UnitLength.meters)) // 0.621 mi

Official documentation: https://developer.apple.com/documentation/foundation/measurementformatter

时光是把杀猪刀 2024-12-11 18:05:50
// Before iOS 16
Locale.current.usesMetricSystem

// iOS 16 and up
Locale.current.measurementSystem == .metric

usesMetricSystem 在 iOS 16 上已弃用

// Before iOS 16
Locale.current.usesMetricSystem

// iOS 16 and up
Locale.current.measurementSystem == .metric

usesMetricSystem is deprecated on iOS 16

梦旅人picnic 2024-12-11 18:05:50

你可能应该在你的应用程序中有一个设置,让你的用户选择——这就是苹果在天气应用程序中所做的。

如果您想选择一个合理的默认值,您可以查看区域设置。如果是美国,则选择英制,否则选择公制。这是一种启发式方法,有时会出错,但这只是可以更改的默认值。

You should probably just have a setting in your app and let your users choose -- this is what Apple does in the Weather app.

If you want to choose a sensible default you could look at the locale. If it's US, pick imperial otherwise choose metric. It is a heuristic, it will be wrong sometimes, but it's just a default that can be changed.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文