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

发布于 2024-11-14 05:25:10 字数 1088 浏览 3 评论 0原文

实际上,我想显示所有货币代码的货币符号,并且我正在使用这样的代码,但我只得到“$”符号

-(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);   
}

这是正确的还是有其他方法可以做到这一点?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

太阳男子 2024-11-21 05:25:10

这是一个直接的高成本解决方案

+(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;
}

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;
}
两人的回忆 2024-11-21 05:25:10

来自可可手册:因此,通常情况下,您应该使用排出而不是释放。实际上你根本不需要 NSAutoreleasePool 。但这不是你的问题的原因。问题出在语言环境中。 NSNumberFormatter 有指定的区域设置。如果您确实想使用 NSNumberFormatter,您应该在发送 currencySymbol 消息之前更改您的区域设置。
但我建议您像在第一个代码中一样使用 NSLocale

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

在发布之前我已经检查了此代码两次。

From cocoa manual: Typically, therefore, you should use drain instead of release. Actually you do not need a NSAutoreleasePool here at all. But it is not a reason of your problem. The problem is in locale. NSNumberFormatter has an assigned locale. If you do want to use NSNumberFormatter you should change your locale before sending currencySymbol message.
But I advice you to use NSLocale as you did in your first code:

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

I've checked this code twice before posting.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文