我可以从NSLocale获取本地货币的代码吗?我试过这个:我可以从NSLocale获取本地货币的代码吗?
NSLocale *locale = [[[NSLocale alloc] initWithLocaleIdentifier:@"fr_FR"] autorelease];
NSString *currencyCode = [locale objectForKey:NSLocaleCurrencyCode];
但currencyCode返回为零(我想要“EUR”,或类似的)。
我可以从NSLocale获取本地货币的代码吗?我试过这个:我可以从NSLocale获取本地货币的代码吗?
NSLocale *locale = [[[NSLocale alloc] initWithLocaleIdentifier:@"fr_FR"] autorelease];
NSString *currencyCode = [locale objectForKey:NSLocaleCurrencyCode];
但currencyCode返回为零(我想要“EUR”,或类似的)。
在普通的Macintosh应用程序中,您的代码对我来说工作得非常好。
NSLocale *locale = [[[NSLocale alloc] initWithLocaleIdentifier:@"fr_FR"] autorelease];
NSString *currencyCode = [locale objectForKey:NSLocaleCurrencyCode];
NSLog(@"currencyCode is %@", currencyCode);
这显示在控制台:
2011-11-14 14:53:33.784 Testing[26388:707] currencyCode is EUR
你们是不是要建立这个作为一个命令行(基金会)的应用程序或一些非传统的Mac样?
NSLocale
有一个称为- objectForKey:
方法,返回对应于作为一个参数传递的特定 NSLocale
部件键的对象。随着雨燕2.2,- objectForKey:
有如下声明:
func objectForKey(_ key: AnyObject) -> AnyObject?
在众多NSLocale
component keys,苹果指出约NSLocaleCurrencyCode
:
与该区域相关联的货币代码的关键。相应的值是一个
NSString
对象;例如“USD”。
因此,你可以检索链接到NSLocale
实例有以下雨燕2.2和斯威夫特3代码段的币种代码:
雨燕2.2
import Foundation
let locale = NSLocale(localeIdentifier: "fr_FR")
//let locale = NSLocale.currentLocale()
let localeCurrencyCode = locale.objectForKey(NSLocaleCurrencyCode) as! String
print(localeCurrencyCode) // prints "EUR"
斯威夫特3
import Foundation
let locale = Locale(localeIdentifier: "fr_FR")
//let locale = Locale.current
let localeCurrencyCode = locale.object(forKey: Locale.Key.currencyCode) as! String
print(localeCurrencyCode) // prints "EUR"
这个工作适合我。这不是内存问题? – Rodrigo