Reputation: 1477
I'm new with Core Data, and I don't know how to proceed to do what I want. Actually, I would like to make a model with countries and country codes.
BUT: the countries are localized (3 languages for now). So, here is my model at this time:
Entity: Country Attributes:
- name_en (String)
- name_nl (String)
- name_fr (string)
Now, I would like associate country codes for each country.
Example:
- Belgium => BE
- United States => USA
- France => FR
etc. but it has to work in French and Dutch too.
I don't know how to link this country codes to name_en, name_nl and name_fr.
Important: The countries are at the same index in French, English or Dutch:
Belgium (en) => index 3
België (nl) => index 3
Belgique (fr) => index 3
Hope I've been clear ;)
Thank you guys.
Upvotes: 0
Views: 52
Reputation: 80265
I would advise to use the NSLocale
APIs to cover most of this logic. That would greatly simplify your data structure and instead put the complexity where it belongs - and where Apple can do the heavy lifting for you.
The model would be as simple as this: Entity Country
with a string attribute countryCode
, which is the string used by Apple to identify each country. These are strings like
en_US
fr_BE
nl_BE
nl_NL
fr_FR
If you do not want duplicate entries for the same country with different language region, you could split them into Country
and Language
and construct the identifier yourself.
You can generate all the rest with Apple's APIs. E.g., the country code
Country *belgium; // assume localeIdentifier "fr_BE" for French
NSLocale *locale = [NSLocale localeWithLocaleIdentifier:belgium.localeIdentifier];
NSString *belgiumCountryCode = [locale objectForKey:NSLocaleCountryCode];
// --> "BE"
NSString *nameOfBelgiumInFrench =
[locale displayNameForKey:NSLocaleCountryCode value:belgiumCountryCode];
// --> "Belgique"
NSLocale *usLocale = [NSLocale localeWithLocaleIdentifier:@"en_US"];
NSString nameOfBelgiumInEnglish =
[usLocale displayNameForKey:NSLocaleCountryCode value:belgiumCountryCode];
// --> "Belgium"
You can put those cumbersome methods into convenience accessors of your managed object subclasses. Theoretically you could also hard-code them, but I would strongly recommend against it.
One last thing: if you are planning on using Core Data, forget about indexes.
Upvotes: 1