简体   繁体   English

在 iPhone SDK 中使用货币代码显示货币符号

[英]Display currency symbols using currency codes in iPhone SDK

Actually, I want to display the currency symbols of all currency codes and I'm using code like this,but i only get "$" symbols实际上,我想显示所有货币代码的货币符号,我正在使用这样的代码,但我只得到“$”符号

-(void) showCurrenciesList
{
    NSNumberFormatter *numFormatter = [[NSNumberFormatter alloc] init];                         
    [numFormatter setNumberStyle:NSNumberFormatterCurrencyStyle];
    //[numFormatter setLocale: [NSLocale currentLocale]];

    NSMutableArray *aryAllCurrencies = [[NSMutableArray alloc] init]; 
    //NSLocale *locale = [[[NSLocale alloc] initWithLocaleIdentifier: @"en_US"] autorelease];

    NSArray *currencyArray = [NSLocale ISOCurrencyCodes]; 
    NSLog(@"Currency array : %@",currencyArray);

    for (NSString *currencyCode in currencyArray) 
    {
        NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
        [numFormatter setCurrencyCode:currencyCode];
        NSString *currencySymbol = [numFormatter currencySymbol];
        [aryAllCurrencies addObject:currencySymbol];
        [pool release];     
    }   
    //[countriesArray sortUsingSelector:@selector(compare:)];
    NSLog(@"currencies array : %@",aryAllCurrencies);   
}

Is this right or is there another way to do this?这是正确的还是有其他方法可以做到这一点?

It's a straight high cost solution这是一个直接的高成本解决方案

+(NSString *)getCurrencySybolByCurrecyCode:(NSString *)code
{
    NSArray *locales = [NSLocale availableLocaleIdentifiers];
    for (NSString *currentLocale in locales)
    {
        NSLocale *currentLoc = [[[NSLocale alloc] initWithLocaleIdentifier:currentLocale] autorelease];
        if([[currentLoc objectForKey:NSLocaleCurrencyCode] isEqualToString:code])
        {
            NSLog(@"find symbol for code = %@ %@", [currentLoc objectForKey:NSLocaleCurrencyCode], [currentLoc objectForKey:NSLocaleCurrencySymbol]);
            return [currentLoc objectForKey:NSLocaleCurrencySymbol];        }
    }
    return nil;
}

From cocoa manual: Typically, therefore, you should use drain instead of release .来自 cocoa 手册: Typically, therefore, you should use drain instead of release Actually you do not need a NSAutoreleasePool here at all.实际上,您根本不需要NSAutoreleasePool But it is not a reason of your problem.但这不是您的问题的原因。 The problem is in locale.问题出在语言环境中。 NSNumberFormatter has an assigned locale. NSNumberFormatter有一个指定的语言环境。 If you do want to use NSNumberFormatter you should change your locale before sending currencySymbol message.如果您确实想使用NSNumberFormatter ,您应该在发送currencySymbol消息之前更改您的语言环境。 But I advice you to use NSLocale as you did in your first code:但我建议您像在第一个代码中那样使用NSLocale

NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier: @"en_US"];
NSString *dollar = [locale displayNameForKey:NSLocaleCurrencyCode value:@"USD"];
[locale release];

I've checked this code twice before posting.在发布之前,我已经检查了此代码两次。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM