我正在为多个国家设置应用内购买商店。如何确定需要以美元、欧元等货币显示价格?我认为这与localeIdentifier有关,但我不确定该如何处理。
你可以使用(NS)Locale
获取货币符号和代码。
Swift 1 和 2
let locale = NSLocale.currentLocale()
let currencySymbol = locale.objectForKey(NSLocaleCurrencySymbol)!
let currencyCode = locale.objectForKey(NSLocaleCurrencyCode)!
Swift 3
let locale = Locale.current()
let currencySymbol = locale.object(forKey: .currencySymbol)!
let currencyCode = locale.object(forKey: .currencyCode)!
Swift 3.1+
:Swift 3.1及以上版本。let locale = Locale.current
let currencySymbol = locale.currencySymbol!
let currencyCode = locale.currencyCode!
这与用户地区格式设置相关,在 iOS
和 macOS
上都适用。
针对Swift3
//User region setting return
let locale = Locale.current //NSLocale.current
//Returns true if the locale uses the metric system (Note: Only three countries do not use the metric system: the US, Liberia and Myanmar.)
let isMetric = locale.usesMetricSystem
//Returns the currency code of the locale. For example, for “zh-Hant-HK”, returns “HKD”.
let currencyCode = locale.currencyCode
//Returns the currency symbol of the locale. For example, for “zh-Hant-HK”, returns “HK$”.
let currencySymbol = locale.currencySymbol
适用于iOS 16+
//Returns the user's current locale
let locale = Locale.current
//Returns the local currency's symbol
//Ex: $
let currencySymbol = locale.currencySymbol!
//Returns the currency code of the locale. Returns nil if the data isn't available
//Ex: USD
let currencyCode = locale.currency!.identifier
Locale
文档。 - vadianLocale.current()
变成了Locale.current
。 :) - HAS