Finding the distance, currency, temperature units etc. for a country (iPhone)
Asked Answered
S

5

24

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.

Swetlana answered 28/1, 2010 at 9:0 Comment(0)
S
30

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.

Scrimpy answered 28/1, 2010 at 9:17 Comment(3)
Hi Kenny Thank You! Guess I'll give it some more thought to as if it can be justified loading such a list for two small labels :/Swetlana
Hi Kenny! Thanks for getting back on this one:) just perfect, I had not yet begun compiling a list of countries vs. measurement system so this came at a perfect time. Thanks again.Swetlana
It is actual as simple as: BOOL isMetric = [[[NSLocale currentLocale] objectForKey:NSLocaleUsesMetricSystem] boolValue];Swetlana
T
9

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

Tripp answered 5/5, 2014 at 6:54 Comment(0)
L
8

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
Leavy answered 9/9, 2014 at 9:34 Comment(0)
F
2

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)
Fourier answered 23/7, 2015 at 16:25 Comment(0)
T
0

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)
Television answered 7/9, 2018 at 13:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.