2016-06-20 34 views
8

Tôi đang thiết lập một chút trong cửa hàng mua ứng dụng cho các quốc gia khác. Làm thế nào tôi có thể tìm ra rằng tôi phải hiển thị giá trong Dollar, Euro vv ... Tôi nghĩ rằng nó phải làm với localeIdentifier nhưng tôi không chắc chắn làm thế nào để xử lý nàyNhận tiền nội tệ nhanh chóng

Trả lời

33

Bạn có thể nhận được biểu tượng tiền tệ và mã từ (NS)Locale với

Swift 1 và 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

let locale = Locale.current 
let currencySymbol = locale.currencySymbol! 
let currencyCode = locale.currencyCode! 

này tương quan với các thiết lập định dạng khu vực sử dụng và làm việc tại cả iOSmacOS

+0

Với Xcode beta 4 (Swift 3) Tôi 'm nhận được lỗi: Giá trị của loại' Locale 'không có thành viên' đối tượng ' – blwinters

+0

@ blwinters tôi nghĩ rằng nhanh chóng 3 tương đương là Locale.current.regionCode – bolnad

+0

@blwinters Cú pháp có thể thay đổi tất cả thời gian trong giai đoạn beta. Vui lòng đọc tài liệu về 'Locale' trong phiên bản Xcode thích hợp. – vadian

2

cho 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 
Các vấn đề liên quan