0

So, consider the following function:

+ (NSLocale *)localeWithCurrencyCode:(NSString *)currencyCode
{
    for (NSString *localeIdentifier in [NSLocale availableLocaleIdentifiers])
    {
        NSLocale *locale=[[NSLocale alloc] initWithLocaleIdentifier:localeIdentifier];

        if ([[locale objectForKey:NSLocaleCurrencyCode] isEqualToString:currencyCode])
        {
            return locale;

        }

    }
    ////

    return [[NSLocale alloc] initWithLocaleIdentifier:[NSLocale localeIdentifierFromComponents:[NSDictionary dictionaryWithObject:currencyCode forKey:NSLocaleCurrencyCode]]]; // For some codes that locale cannot be found, initialize manually!

}

This sort of works, but it doesn't work as desired. Because other countries share currencies (for example, the U.S. Dollar is used in Panama, Ecuador, etc.) ... it returns alphabetically the first country for said currency, which is incorrect.

Is there a better way to build some sort of function like this without incorporating some sort of external or third-party database of the primary country for each currency? Hoping to avoid external dependencies.

Ben Guild
  • 4,881
  • 7
  • 34
  • 60
  • What is it you actually want? Also a better example of a shared currency would be the Euro, not the US Dollar. – trojanfoe Mar 29 '16 at 06:18
  • According to Wikipedia, Ecuador actually _does_ use the US dollar (unlike say Canada, which uses its own dollar. That can be confusing enough). Of course with the Euro, there is definitely nothing that could be called a "primary" country. – gnasher729 Mar 29 '16 at 07:54
  • Right, so the Euro would be an exception as it doesn't belong to a country. But, what I'm looking for, is the primary country for a currency. – Ben Guild Mar 29 '16 at 08:39
  • I'm almost thinking it's better to hardcode the instances where currencies are shared. Like EUR, USD. – Ben Guild Mar 30 '16 at 02:36

0 Answers0