Definite article before country name in sentence - ios

I have an iOS app where I want to include the user's country (as defined by region) in a text string, e.g. “In Portugal this is …”. For most country names this is easy:
let countryCode = locale.objectForKey(NSLocaleCountryCode) as! String
let countryName = locale.displayNameForKey(NSLocaleCountryCode,
value: countryCode)!
let text = "In \(countryName) this is …"
However, for some country names, the name should be prefixed by “the”, for example “the Netherlands” and “the United States”. Is this information available in NSLocale somehow, or are there other libraries that can help with this?

Related

How to filter GMSPlacesAutocompleteTypeFilter to obtain the Country result for both United states and canada

let filter = GMSAutocompleteFilter()
filter.type = GMSPlacesAutocompleteTypeFilter.city
filter.country = "USA" || "CA"
how to get 2 country results in google autocomplete
If you check country property in GMSAutocompleteFilter class then you will found that it is string so I think you can't set multiple country in your filter. If it's allowing multiple country then it should be array or any collection instead of string! and in documentation also they haven't mentioned for multiple countries in filter! So, I think you can't set more than one country at a time in one filter!
It's currently not possible. I would recommend using the web API, where you can send up to 5 ISO country codes in your query (see components under "optional parameters":
https://developers.google.com/places/web-service/autocomplete
It's possible to do using following syntax
filter.country = "CA|country:US"
reference link here.

How would you match two similar phone numbers in Swift, but one contains the country code and the other one does or does not

I am trying to match two similar phone numbers in swift, but one of them is preceded by a country code and the other one can vary.
For instance, I want these 2 phone numbers to match:
0499999999
+32499999999
I want this to be valid for any phone number all over the world. Is there a regex for this or maybe a pod I could download for doing this?
Thanks
Use PhoneNumberKit To Parse PhoneNumber.
https://github.com/marmelroy/PhoneNumberKit
Like :
let rawNumberArray = ["0291 12345678", "+49 291 12345678", "04134 1234", "09123 12345"]
let phoneNumbers = phoneNumberKit.parse(rawNumberArray)
let phoneNumbersCustomDefaultRegion = phoneNumberKit.parse(rawNumberArray, withRegion: "DE", ignoreType: true)
You could make a lookup table of countries and country codes and check from the phone what their locale is
let countrycode = NSLocale.currentLocale().objectForKey(NSLocaleCountryCode) as? String
let countryPrefixes = ["IE": "+353",
"US": "+1",
etc...
let prefix = countryPrefixes[countrycode]
Once you have the prefix, you should be able to look at just the remaining numbers.
Or you could use this github library https://github.com/rmaddy/RMPhoneFormat which seems to have that work done for you already.
Of course this is assuming that you are expecting a phone number entered into your device that is valid in the Locale that the phone is operating in.

How can I sort an array into a dictionary?

Let's say I have an array which contain multiple country names such as "Australia, Denmark, United Kingdom, Austria, Australia, Denmark" some of the country names appear twice.
How can I sort them to form a dictionary based on country names. So they key would be the country name and the element would be the country.
If I have two countries in my array that are the same, the key would be the country and the elements would be those two countries.
I need it so that if I add another country it will make a key for the country without having to specify keys beforehand.
Each country needs to be under a key of it's country, not dependent on the occurrences of the country in the array.
I think I've worked out a basic algorithm for it but I can't seem to put it into practice.
While enumerating over the array
check to see if a key in the dictionary matches the current string
If it does, add the string to the dictionary under the matching key
If it doesn't create a key and place the string under the key.
Is this algorithm correct or at least a step in the right direction?
Thanks for the help.
EDIT:
We have an array which contains the country names "Australia, Denmark, United Kingdom, Austria, Australia, Denmark"
I need to organise this into a dictionary based on countries so as we have two of the country Denmark in the array I need to sort it so it looks like this:
Denmark: "Denmark", "Denmark"
The key is the country name and the element is the string.
United Kingdom only occurs once so that part of the dictionary will look like this:
United Kingdom: "United Kingdom"
I hope I've made more sense.
Not sure if this is what you meant. It's not very clear.
var dict = [String: [String]]()
let countries = ["Holland", "England", "France", "Belgium", "England"]
for country in countries {
dict[country] = (dict[country] ?? []) + [country]
}
for (key, value) in dict {
println("KEY: \(key) & VALUE: \(value)")
}
Output:
KEY: England & VALUE: [England, England]
KEY: Belgium & VALUE: [Belgium]
KEY: Holland & VALUE: [Holland]
KEY: France & VALUE: [France]
EDIT:
Simplified based on Martin R's link in his comment.
The simplest way is to just loop over the array and check if the key exists in the dictionary. Make each value of the dictionary an NSMutableArray.

CoreData : suggestions for model design

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.
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.

Contact address format for different countries

In an app, I am displaying the details of the contacts using ABRecordRef. I am using the keys kABPersonAddressCityKey, kABPersonAddressStateKey, kABPersonAddressZIPKey, kABPersonAddressCountryKey. Everything works fine. But I don't know in which format to display the addresses. What I mean is, if you see the Contacts app, the addresses are displayed in a particular format for different countries. Some examples,
US
Street
City State ZIP
Country
India
Street
Province
City PIN
Country
Australia
Street
Suburb State ZIP
Country
Now I don't know how to find the format for different countries.
1.Is there any way to find the address format based on country/country codes?
2.Is there a way we can get the fully formatted address using a single function, like we use ABRecordCopyCompositeName() to get the full name?
From iOS 9.0 / macOS 10.11 / watchOS 2.0 on you should use CNPostalAddressFormatter instead:
The CNPostalAddressFormatter class formats the postal address in a contact. This class handles international formatting of postal addresses.
Below code is in Swift 3 but it is trivial to convert it to Objc
let postalAddress = CNMutablePostalAddress()
postalAddress.street = street
postalAddress.postalCode = zipCode
postalAddress.city = city
postalAddress.state = state
postalAddress.country = country
postalAddress.isoCountryCode = countryCode
let formattedAddress = CNPostalAddressFormatter.string(from: postalAddress, style: .mailingAddress)
Make sure you set the ISO country code property, this is used to determine the format of the address.
Example:
postalAddress.street = "Main Street 1"
postalAddress.postalCode = "67067"
postalAddress.city = "Ludwigshafen"
postalAddress.state = "Rhineland-Palatinate"
postalAddress.country = "Germany"
postalAddress.isoCountryCode = "DE"
leads to this
Main Street 1
67067 Ludwigshafen
Germany
whereas
postalAddress.isoCountryCode = "US"
leads to
Main Street 1
Ludwigshafen Rhineland-Palatinate 67067
Germany
Try this link
ABCreateStringWithAddressDictionary
It looks like you need to use: ABCreateStringWithAddressDictionary that will return:
Returns a formatted address from an address property. (From Apple)
Good luck

Resources