Simon Simon - 4 months ago 16
iOS Question

Can I get the code for the local currency from an NSLocale?

Can I get the code for the local currency from an NSLocale? I tried this:

NSLocale *locale = [[[NSLocale alloc] initWithLocaleIdentifier:@"fr_FR"] autorelease];
NSString *currencyCode = [locale objectForKey:NSLocaleCurrencyCode];


but currencyCode comes back as nil (I wanted "EUR", or similar).

Answer

Your code works perfectly fine for me in a regular Macintosh app.

NSLocale *locale = [[[NSLocale alloc] initWithLocaleIdentifier:@"fr_FR"] autorelease];
NSString *currencyCode = [locale objectForKey:NSLocaleCurrencyCode];
NSLog( @"currencyCode is %@", currencyCode );

displays this in the console:

2011-11-14 14:53:33.784 Testing[26388:707] currencyCode is EUR

Are you trying to build this as a command line (Foundation) app or something non-traditional-Mac-like?