查找某个国家/地区的距离、货币、温度单位等 (iPhone)

发布于 2024-08-19 14:47:41 字数 200 浏览 8 评论 0原文

在我的应用程序的本地化过程中,我浏览了很多文档,我确信我在某个地方读到有一种方法可以获取一组与区域设置链接的单元。

我相信它说了类似“基于文化的单位……”之类的东西。

我想以华氏度和英里为单位显示某些国家的温度和距离,以摄氏和公里为其他国家的温度和距离。

有没有办法访问 iPhone SDK 中这些单位的列表。

感谢您。

During the localization of my app I went through a lot of documentation, somewhere Im sure I read that there is a way to get a set of units that is linked with the locale.

I believe it said something like "units based on cultural..." something.

I would like to display temperature and distance in Fahrenheit and Miles for some countries and Celsius and Kilometers for other countries.

Is there a way to access a list of these units in the iPhone SDK.

Thanks you.

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

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

发布评论

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

评论(5

-小熊_ 2024-08-26 14:47:41

您可以使用 NSLocale 检查货币单位,但是对于英制与公制,您需要自己制作一个列表

哎呀。您可以检查英制与公制。有 NSLocaleMeasurementSystemNSLocaleUsesMetricSystem 键。

You can use NSLocale to check the currency unit, but for imperial vs. metric you need to make a list yourself.

Oops. You can check for the imperial vs. metric. There is are NSLocaleMeasurementSystem and NSLocaleUsesMetricSystem keys.

零時差 2024-08-26 14:47:41

值得一提的是,Apple 提供了一个很好的库(MKDistanceFormatter,在 iOS 7 中引入)来自动将 CLLocationDistance 作为给定用户的区域设置和语言的适当字符串呈现。

它进行了一些模糊舍入(例如 300 英尺 -> 350 英尺 -> 400 英尺),但可以很好地在单位之间进行转换(例如英尺 -> 英里,在适当的情况下)。除非您需要非常精确的值,否则此类非常适合用两行代码返回距离的粗略估计。

MKDistanceFormatter *distanceFormatter = [[MKDistanceFormatter alloc] init];
[distanceFormatter setUnitStyle:MKDistanceFormatterUnitStyleAbbreviated]; // Optional
NSString *formattedDistance = [distanceFormatter stringFromDistance:100];
NSLog(@"%@",formattedDistance); // Prints "350 ft"

https://developer.apple.com/library /ios/documentation/MapKit/Reference/MKDistanceFormatter_class/Reference/Reference.html

It is worth mentioning that Apple provides a nice library (MKDistanceFormatter, introduced in iOS 7) to automatically present a CLLocationDistance as the appropriate string for a given user's locale and language.

It does a little fuzzy rounding (e.g. 300ft->350ft->400ft), but does nicely convert between units (e.g. feet -> miles, when appropriate to do so). Unless you demand really precise values, this class is perfect for returning rough estimates for distances in two lines of code.

MKDistanceFormatter *distanceFormatter = [[MKDistanceFormatter alloc] init];
[distanceFormatter setUnitStyle:MKDistanceFormatterUnitStyleAbbreviated]; // Optional
NSString *formattedDistance = [distanceFormatter stringFromDistance:100];
NSLog(@"%@",formattedDistance); // Prints "350 ft"

https://developer.apple.com/library/ios/documentation/MapKit/Reference/MKDistanceFormatter_class/Reference/Reference.html

甩你一脸翔 2024-08-26 14:47:41

我只是想指出,随着 iOS8 的发布,至少您提到的某些单元将会有新的 NSFormatters

将会有 NSLengthFormatterNSMassFormatterNSEnergyFormatter 它们非常容易使用 - 请参阅此 iOS8 NSFormatter 教程。下面是一个使用 NSLengthFormatter 和 swift 的例子:

let lengthFormatter = NSLengthFormatter()
println(@"Kilometer: %@", lengthFormatter.stringFromValue(1000, unit: .Kilometer))       //Kilometer: 1,000 km

I just wanted to note, that, as with the release of iOS8 there will be new NSFormatters for at least some of the units you mentioned.

There will be NSLengthFormatter, NSMassFormatter and NSEnergyFormatter which are very easy to use – see this iOS8 NSFormatter tutorial. Here is an example using the NSLengthFormatter with swift:

let lengthFormatter = NSLengthFormatter()
println(@"Kilometer: %@", lengthFormatter.stringFromValue(1000, unit: .Kilometer))       //Kilometer: 1,000 km
为你鎻心 2024-08-26 14:47:41

Swift:

1.)

import MapKit

2.) 由于创建格式化程序的成本很高,因此最好将格式化程序设置为惰性属性。通过这样做,您可以仅在必要时初始化格式化程序并重用格式化程序而不是创建新的格式化程序。

lazy var distanceFormatter: MKDistanceFormatter = {
    var tempDistanceFormatter =  MKDistanceFormatter()
    tempDistanceFormatter.unitStyle = .Abbreviated // .abbreviated in Swift 3

    return tempDistanceFormatter
}()

3.)

let distanceString = self.distanceFormatter.stringFromDistance(yourDistance)

Swift:

1.)

import MapKit

2.) Since creating formatter is expensive, you better set formatter as a lazy property. By doing so, you can initialize the formatter only when it's necessary and reuse the formatter rather than create a new one.

lazy var distanceFormatter: MKDistanceFormatter = {
    var tempDistanceFormatter =  MKDistanceFormatter()
    tempDistanceFormatter.unitStyle = .Abbreviated // .abbreviated in Swift 3

    return tempDistanceFormatter
}()

3.)

let distanceString = self.distanceFormatter.stringFromDistance(yourDistance)
逐鹿 2024-08-26 14:47:41

我已将此方法提取到 String 扩展。看起来很优雅

Swift 4.1 版本

import MapKit

extension String {
    init(distance: Double ) {
        let mkDistanceFormatter = MKDistanceFormatter()
        mkDistanceFormatter.unitStyle = .abbreviated
        self = mkDistanceFormatter.string(fromDistance: distance)
    }
}

用法

let distance = Double(34234.0)
let stringDistance String(distance:distance)

I have extracted this method to String extension. Looks elegant

Swift 4.1 version

import MapKit

extension String {
    init(distance: Double ) {
        let mkDistanceFormatter = MKDistanceFormatter()
        mkDistanceFormatter.unitStyle = .abbreviated
        self = mkDistanceFormatter.string(fromDistance: distance)
    }
}

Usage

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