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.