NSLocale Swift 3
Asked Answered
A

5

14

How do I get the currency symbol in Swift 3?

public class Currency: NSObject {
    public let name: String
    public let code: String
    public var symbol: String {
        return NSLocale.currentLocale().displayNameForKey(NSLocaleCurrencySymbol, value: code) ?? ""
    }

    // MARK: NSObject

    public init(name: String, code: String) {
        self.name = name
        self.code = code
        super.init()
    }
}

I know NSLocale got renamed to Locale, but displayNameForKey got removed and I only seem to be able to use localizedString(forCurrencyCode: self.code) to generate the name of the currency in the current locale without being able to get its symbol. I'm looking for a way to get a foreign currency symbol in a current locale.

Or am I overlooking something?

Alboran answered 15/9, 2016 at 19:42 Comment(0)
I
24

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$
Ibadan answered 15/9, 2016 at 19:56 Comment(1)
Thank you this works and looks a lot neater than the solution I made by using a global Objective-C function.Alboran
S
5
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.

Savarin answered 15/9, 2016 at 19:56 Comment(2)
That only works for the currency symbol of the current locale.Alboran
I was looking for a foreign currency symbol in the current locale. Not for a foreign currency code in a foreign locale.Alboran
A
2

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")
Atelectasis answered 26/5, 2017 at 2:36 Comment(0)
S
0

For swift 3

locale.regionCode

regionsCode is similar to the displayName

Surrender answered 10/7, 2017 at 10:43 Comment(0)
A
0

There is a function to get localized name of a currency code in the Locale directly no need to cast to NSLocale.

Locale(identifier: "en-GB").localizedString(forCurrencyCode: "USD") // "Us Dollar"
Locale(identifier: "pl-PL").localizedString(forCurrencyCode: "USD") // "Dolar Amerykański"
Aware answered 15/11, 2019 at 15:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.