How to get ISOCurrencyCode from a ISOCountryCode in iphone sdk?

断了今生、忘了曾经 提交于 2019-11-29 09:23:27

问题


I have the ISOCountryCode avaliable and now i want to derive the currencyCode of this country from the ISOCountryCode. How can i achieve that?

  NSString *countryCode =  < get from someother view>;     
  NSString *currencyCode = ?;

I receive this country code from some other view on runtime?


回答1:


You will need to use NSLocale. To retrieve the currency code from a specific country code:

NSString *countryCode = @"US";

NSDictionary *components = [NSDictionary dictionaryWithObject:countryCode forKey:NSLocaleCountryCode];
NSString *localeIdent = [NSLocale localeIdentifierFromComponents:components]; 
NSLocale *locale = [[[NSLocale alloc] initWithLocaleIdentifier:localeIdent] autorelease];
NSString *currencyCode = [locale objectForKey: NSLocaleCurrencyCode];

You can also get it for the current locale, ie the user settings:

NSString *currencyCode = [[NSLocale currentLocale] objectForKey: NSLocaleCurrencyCode];



回答2:


NSLocale encapsulates a rich set of domain-specific information, among others you have NSLocaleCurrencyCode:

 if let code = NSLocale.currentLocale().objectForKey(NSLocaleCurrencyCode) {
                print("\(code)")
            }


来源:https://stackoverflow.com/questions/3433895/how-to-get-isocurrencycode-from-a-isocountrycode-in-iphone-sdk

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!