I am currently working on a map-based iPhone application and wish to display some information to the user. For my application, it makes sense to have a setting of some sort where the user can choose Miles or Kilometers. Is there a built in mechanism (maybe similar to string localization) for doing this kind of value switching so that I can avoid an if-block around each time I want to display something to the user?
views:
140answers:
1
+5
A:
Take a look at NSLocale. You should be able to get the current locale using [NSLocale currentLocale]. Then call [theLocale objectForKey: NSLocaleMeasurementSystem] and look at the results which should tell you if the users locale uses the metric system. The docs for NSLocale have a constants section which list all of the values that can be passed to the locale.
Using this you would make your own function that could be used in your program to return your distance in a locale specific way.
Jon Steinmetz
2010-02-22 21:50:36
If you want a simple boolean value, you could also check the NSLocaleUsesMetricSystem key, which returns an NSNumber-wrapped boolean value for whether to use kilometers or miles.
Brad Larson
2010-02-22 21:58:27
In my application, I intend to give the user the option of having things displayed in miles or kilometers. Based on the feedback on this thread, I think it's clear that there is not a built-in mechanism of interchanging one unit for the other, but it won't be too difficult to just have a method that checks the user-set boolean and does a conversion to kilometers. Thanks for the responses. I'm sure they'll come in handy for me at some point.
TahoeWolverine
2010-02-23 13:26:45
This method would still be useful for initially populating your preference with the users system choice for units. Something to consider.
Jon Steinmetz
2010-02-23 13:46:49