我正在使用某种货币格式将货币符号/样式设置为用户的本地设置。这是我的代码。我认为它工作正常。它位于我的viewDidLoad中。在多种方法中使用相同的货币格式
let currencyFormat = NumberFormatter()
currencyFormat.locale = NSLocale.current
currencyFormat.usesGroupingSeparator = true
currencyFormat.numberStyle = NumberFormatter.Style.currency
...
labelTotalAmount.text = currencyFormat.string(for: totalAmount)
问题是,我想在其他两种不同的方法中使用相同的格式。每当我想要格式化时,重复每种方法的格式似乎都是一种浪费。
有没有一种方法来设置格式一次,并让它记住类的每个方法中的设置?
我正在使用Swift 3.我很感谢您可以给予的任何帮助!
使用类或全局值或单例。 – JAL
http://stackoverflow.com/questions/24960621/struggling-with-nsnumberformatter-in-swift-for-currency/42957137#42957137 –
仅供参考 - 无需区域设置为当前语言环境,因为这是默认的。 – rmaddy