Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
508 views
in Technique[技术] by (71.8m points)

swift - Storing Locale to CoreData Swiftui

I would like to store the Locale to Core Data that I can assign it against a list item. Thus when a user changes a locale the formatting of the original currency etc. is not impacted by the change and thus the app still functions.

I think this would be something useful, even if a user would not typically change the region often.

I have defined a currency formatter using current locale settings:

var currencyFormatter: NumberFormatter = {
    let formatter = NumberFormatter()
    formatter.usesGroupingSeparator = true
    formatter.numberStyle = .currency
    formatter.locale = Locale.current
   return formatter
}()

I have a function that uses this to a. format a currency and b. strip it for processing:

func formatCurrency(text: String, edit: Bool, currencyCode: String) -> String {
        if(!edit && text.count > 0) {
             let myDouble = (text as NSString).doubleValue
             let myNumber = NSNumber(value:myDouble)
            let priceString = currencyFormatter.string(from: myNumber) ?? "0.00"
             print(priceString)
             return priceString
        } else {
            let myDouble = (text as NSString).doubleValue
            let myNumber = NSNumber(value:myDouble)
            let returnValue = currencyFormatter.number(from: text) ?? myNumber
            if(returnValue == 0){
                return ""
            }
            return returnValue.stringValue
        }
    }

I have tried to save the 'currencyFormatter.currencyCode` in Core Data as a text, this works, however since you need to consider symbols for decimal, and separators etc. this becomes clumsy as there are lots of parameters. Below are a few:

print("currencyDecimalSeparator - " + currencyFormatter.currencyDecimalSeparator)
 print("currencySymbol - " + currencyFormatter.currencySymbol)
 print("currencyGroupingSeparator - " + currencyFormatter.currencyGroupingSeparator)
 print("internationalCurrencySymbol - " + currencyFormatter.internationalCurrencySymbol)
 print("currencyCode - " + currencyFormatter.currencyCode)
 print("decimalSeparator - " + currencyFormatter.decimalSeparator)

I would like to have a neat way of storing Locale.current that I can assign it back when accessing a specific list item.

Thank you, Etienne


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Thank you guys.

Solution as follow:

  1. I stored the locale identifier in Core Data against my entity that I use to populate a List:

    @State var localeIdentifier = Locale.current.identifier

  2. When I want to format the currency, I use the stored identifier to initiate a new locale for my formatter:

    currencyFormatter.locale = Locale.init(identifier: localeIdentifier)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...