0

Trying to save Locales into CoreData and just wasn't sure what format I should save the data.

let locale = Locale.current
let currencyCodesArray = Locale.commonISOCurrencyCodes

for currencyCode in currencyCodesArray {

        // let currencyName = locale.displayName(forKey:NSLocale.Key.currencyCode, value : currencyCode)
        let currencyName = locale.localizedString(forCurrencyCode: currencyCode)

        //let currencySymbol = locale.displayName(forKey:NSLocale.Key.currencySymbol, value : currencyCode)
        let currencySymbol = locale.currencySymbol

        let identifier = locale.localizedString(forIdentifier: currencyCode)

         print(identifier);

        if let _ = currencySymbol, let currencyName = currencyName{

            let currencyModel = CurrencyModel()
            currencyModel.currencyName = currencyName
            currencyModel.currencyCode = currencyCode
            currencyModel.currencySymbol = currencySymbol!

            //currencyModel.identifier = identifier

            currencies.append(currencyModel)

            //print(identifier);
        }
    }

From each locale I'm trying to save: currency code - string Identifier - String currency name - String currency symbol - String

Should I be saving each of these properties on their own, or is it possible to save the entire Locale?

icekomo
  • 9,328
  • 7
  • 31
  • 59

2 Answers2

1

In Swift it's Locale without NS and without bridge cast

let locale = Locale.current

Save the identifier of the Locale which is a String

let currentIdentifier = Locale.current.identifer

And create a Locale when reading it

let locale = Locale(identifier : currentIdentifier)
vadian
  • 274,689
  • 30
  • 353
  • 361
  • No, they are different. – vadian Sep 12 '18 at 15:18
  • I'm trying to save the identifier for whichever currency the user selects, but right now it only seems to be using my local identifier. I've updated my code above. I think its the line that should be something else: currencyModel.identifier = locale.localeIdentifier – icekomo Sep 12 '18 at 15:41
  • Please stop using `NSLocale`. Use the native Swift struct. – vadian Sep 12 '18 at 15:43
  • What would that look like? When I type it out, code hinting gives me the NSLocale version. Made some updates. One of the issues I'm running into is there doesn't seem to be a displayName property on the Swift Locale. – icekomo Sep 12 '18 at 15:58
  • You can use also the `localizedString(for...` methods. – vadian Sep 12 '18 at 16:07
  • am I on the right path with what I'm doing in the updated code? – icekomo Sep 12 '18 at 17:58
  • I tried to use this: let identifier = locale.localizedString(forIdentifier: currencyCode) but it returns a bunch of nils and a few actual indetifiers – icekomo Sep 13 '18 at 01:59
0

You can save NSLocale as Transformable attribute.

NSKeyedUnarchiveFromDataTransformerName is used by default to transform your plain object to Core Data storage representation. But in this case your plain object should conform NSCoding or NSSecureCoding protocol.

If you want to transform more complex object you have to subclass NSValueTransformer.

But NSLocale conforms to NSSecureCoding so you can save it directly by setting type of this attribute of your Entity to Transformable.

See the sample from Apple.

ezaji
  • 304
  • 2
  • 9