在 iPhone SDK 中使用货币代码显示货币符号
实际上,我想显示所有货币代码的货币符号,并且我正在使用这样的代码,但我只得到“$”符号
-(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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是一个直接的高成本解决方案
It's a straight high cost solution
来自可可手册:
因此,通常情况下,您应该使用排出而不是释放
。实际上你根本不需要 NSAutoreleasePool 。但这不是你的问题的原因。问题出在语言环境中。NSNumberFormatter
有指定的区域设置。如果您确实想使用NSNumberFormatter
,您应该在发送currencySymbol
消息之前更改您的区域设置。但我建议您像在第一个代码中一样使用
NSLocale
:在发布之前我已经检查了此代码两次。
From cocoa manual:
Typically, therefore, you should use drain instead of release
. Actually you do not need aNSAutoreleasePool
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 useNSNumberFormatter
you should change your locale before sendingcurrencySymbol
message.But I advice you to use
NSLocale
as you did in your first code:I've checked this code twice before posting.