Get Currency Symbol based on Country code or Country name using NSLocale

前端 未结 4 677
野性不改
野性不改 2020-12-16 08:11

I want to display Currency Symbol based on Country name or country code using NSLocale I have all the country name list. suppose I have selected USA then it

4条回答
  •  春和景丽
    2020-12-16 08:53

    You can Get currency code from Country name, tested it on swift3, first of all add this extension

    extension NSLocale {
    class func locales1(countryName1 : String) -> String {
        let locales : String = ""
        for localeCode in NSLocale.isoCountryCodes {
            let countryName = (Locale.current as NSLocale).displayName(forKey: .countryCode, value: localeCode)
            if countryName1.lowercased() == countryName?.lowercased() {
                return localeCode
            }
        }
        return locales
    }
    
    }
    

    You will get the currency code from here

            let countryCode = NSLocale.locales1(countryName1: "\(place.name)")
    
            let countryCodeCA = countryCode 
            let localeIdCA = NSLocale.localeIdentifier(fromComponents: [ NSLocale.Key.countryCode.rawValue : countryCodeCA])
            let localeCA = NSLocale(localeIdentifier: localeIdCA)
            let currencySymbolCA = localeCA.object(forKey: NSLocale.Key.currencySymbol)
            let currencyCodeCA = localeCA.object(forKey: NSLocale.Key.currencyCode)
    
            print("\(currencyCodeCA!)")
            self.currencyKey = currencyCodeCA! as! String
    

提交回复
热议问题