NSLocale Swift 3

给你一囗甜甜゛ 提交于 2019-11-29 09:22:46

NSLocale was not renamed, it still exists. Locale is a new type introduced in Swift 3 as a value type wrapper (compare SE-0069 Mutability and Foundation Value Types).

Apparently Locale has no displayName(forKey:value:) method, but you can always convert it to its Foundation counterpart NSLocale:

public var symbol: String {
    return (Locale.current as NSLocale).displayName(forKey: .currencySymbol, value: code) ?? ""
}

More examples:

// Dollar symbol in the german locale:
let s1 = (Locale(identifier:"de") as NSLocale).displayName(forKey: .currencySymbol, value: "USD")!
print(s1) // $

// Dollar symbol in the italian locale:
let s2 = (Locale(identifier:"it") as NSLocale).displayName(forKey: .currencySymbol, value: "USD")!
print(s2) // US$
Locale.current.currencySymbol

The new Locale type moved most of the stringly typed properties into real properties. See the developer pages for the full list of properties.

I use extension for Locale this is my code

extension Int {
func asLocaleCurrency(identifier: String) -> String {
    let formatter = NumberFormatter()
    formatter.numberStyle = .currency
    formatter.locale = Locale(identifier: identifier)
    return formatter.string(from: NSNumber(integerLiteral: self))!
}
}

and this for use

var priceCount = 100000
priceCount.asLocaleCurrency(identifier: "id_ID")

For swift 3

locale.regionCode

regionsCode is similar to the displayName

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!