Iphone, Obtaining a List of countries in an NSArray
Use [[NSLocale currentLocale] displayNameForKey:NSLocaleCountryCode value:countryCode]
(where countryCode is an item in your list of country codes) to get the country's name in the user's current locale.
Iphone, Obtaining a List of countries from MonoTouch
Sadly a quick look shows that displayNameForKey:value: to be (currently as of 4.2.x) missing from MonoTouch (and MonoMac) bindings. I'll look into implementing it and will update this entry once done.
UPDATE : Source code to work around the missing binding
public void DisplayCountryCodeNames ()
{
NSLocale current = NSLocale.CurrentLocale;
IntPtr handle = current.Handle;
IntPtr selDisplayNameForKeyValue = new Selector ("displayNameForKey:value:").Handle;
foreach (var countryCode in NSLocale.ISOCountryCodes) {
using (var key = new NSString ("kCFLocaleCountryCodeKey")) {
using (var nsvalue = new NSString (countryCode)) {
string ret = NSString.FromHandle (MonoTouch.ObjCRuntime.Messaging.IntPtr_objc_msgSend_IntPtr_IntPtr (handle, selDisplayNameForKeyValue, key.Handle, nsvalue.Handle));
Console.WriteLine ("{0} -> {1}", countryCode, ret);
}
}
}
}
Adapt to your liking and have fun with MonoTouch!
p.s. I'll update the bindings so it will be included in future releases for MonoTouch in a more proper API ;-)
How to get a list of country names based for each LocaleIdentifier?
Here is a list of locale identifiers and their country name:
https://gist.github.com/jacobbubu/1836273
Get States list of states according to selected country
You can download this sqlite file. This is a database of countries with their respective states. you can re - create a query every time the user selects a country.
NSString *queryString = [NSString stringWithFormat:@"select zone_id , name from
zone where country_id = %d", countryID];
Sort countries per continent
You can use this:
private static HashMap<String,String> countryMap=new HashMap<String,String>();
private final static String EUROPE="E";
private final static String NORTH_AMERICA="N";
private final static String SOUTH_AMERICA="S";
private final static String AFRICA="AF";
private final static String ASIA="AS";
private final static String OCEANIA="O";
private final static String ANTARTIDA="ANT";
static
{
countryMap.put("AF", "AS");
countryMap.put("AL", "E");
countryMap.put("DE", "E");
countryMap.put("DZ", "E");
countryMap.put("AD", "E");
countryMap.put("AQ", "ANT");
countryMap.put("AG", "S");
countryMap.put("SA", "AS");
countryMap.put("AR", "S");
countryMap.put("AM", "AS");
countryMap.put("AW", "S");
countryMap.put("AU", "O");
countryMap.put("AZ", "AS");
countryMap.put("AT", "E");
countryMap.put("BS", "S");
countryMap.put("BH", "AS");
countryMap.put("BD", "AS");
countryMap.put("BB", "S");
countryMap.put("BZ", "S");
countryMap.put("BJ", "AF");
countryMap.put("BM", "S");
countryMap.put("BE", "E");
countryMap.put("BT", "AS");
countryMap.put("BE", "E");
countryMap.put("BO", "S");
countryMap.put("BW", "AF");
countryMap.put("BA", "E");
countryMap.put("BR", "S");
countryMap.put("BN", "AS");
countryMap.put("BG", "E");
countryMap.put("BF", "AF");
countryMap.put("BI", "AF");
countryMap.put("KH", "AS");
countryMap.put("CM", "AF");
countryMap.put("CA", "N");
countryMap.put("CV", "AF");
countryMap.put("BQ", "S");
countryMap.put("AF", "AS");
countryMap.put("CO", "S");
countryMap.put("KM", "AF");
countryMap.put("CG", "AF");
countryMap.put("CD", "AF");
countryMap.put("KP", "AS");
countryMap.put("KR", "AS");
countryMap.put("CR", "S");
countryMap.put("CI", "AF");
countryMap.put("HR", "E");
countryMap.put("CU", "S");
countryMap.put("CW", "S");
countryMap.put("DK", "E");
countryMap.put("DJ", "AF");
countryMap.put("DM", "S");
countryMap.put("EG", "AF");
countryMap.put("SV", "S");
countryMap.put("EC", "S");
countryMap.put("ER", "AF");
countryMap.put("SK", "E");
countryMap.put("SI", "E");
countryMap.put("ES", "E");
countryMap.put("US", "N");
countryMap.put("EE", "E");
countryMap.put("ET", "AF");
countryMap.put("FJ", "O");
countryMap.put("PH", "AS");
countryMap.put("FI", "E");
countryMap.put("FR", "E");
countryMap.put("GA", "AF");
countryMap.put("GM", "AF");
countryMap.put("GE", "E");
countryMap.put("GH", "AF");
countryMap.put("GI", "E");
countryMap.put("GD", "S");
countryMap.put("GL", "N");
countryMap.put("GP", "S");
countryMap.put("GF", "S");
countryMap.put("GU", "AF");
countryMap.put("GT", "S");
countryMap.put("GG", "E");
countryMap.put("GN", "AF");
countryMap.put("GW", "AF");
countryMap.put("GQ", "AF");
countryMap.put("GY", "S");
countryMap.put("HT", "S");
countryMap.put("HN", "S");
countryMap.put("HN", "E");
countryMap.put("IE", "AS");
countryMap.put("BV", "ANT");
countryMap.put("CX", "O");
countryMap.put("HM", "ANT");
countryMap.put("AF", "O");
countryMap.put("IM", "E");
countryMap.put("RE", "AF");
countryMap.put("KY", "S");
countryMap.put("CC", "O");
countryMap.put("CK", "O");
countryMap.put("FO", "E");
countryMap.put("GS", "ANT");
countryMap.put("FK", "S");
countryMap.put("MP", "O");
countryMap.put("MH", "O");
countryMap.put("UM", "O");
countryMap.put("PN", "S");
countryMap.put("SB", "O");
countryMap.put("TC", "S");
countryMap.put("VG", "S");
countryMap.put("VI", "S");
countryMap.put("ID", "AS");
countryMap.put("IR", "AS");
countryMap.put("IQ", "AS");
countryMap.put("IE", "E");
countryMap.put("IS", "E");
countryMap.put("IL", "AS");
countryMap.put("IT", "E");
countryMap.put("IN", "AS");
countryMap.put("JM", "S");
countryMap.put("JP", "AS");
countryMap.put("JE", "E");
countryMap.put("JO", "AS");
countryMap.put("KZ", "AS");
countryMap.put("KE", "AF");
countryMap.put("KG", "AS");
countryMap.put("KI", "O");
countryMap.put("KW", "AS");
countryMap.put("LA", "AS");
countryMap.put("LV", "E");
countryMap.put("LR", "AF");
countryMap.put("LI", "E");
countryMap.put("LT", "E");
countryMap.put("LB", "AS");
countryMap.put("LY", "AF");
countryMap.put("LU", "E");
countryMap.put("MK", "E");
countryMap.put("MG", "AF");
countryMap.put("MW", "AF");
countryMap.put("MV", "AS");
countryMap.put("ML", "AF");
countryMap.put("MT", "E");
countryMap.put("MA", "AF");
countryMap.put("MQ", "S");
countryMap.put("MU", "AF");
countryMap.put("MR", "AF");
countryMap.put("YT", "AF");
countryMap.put("MX", "N");
countryMap.put("FM", "O");
countryMap.put("MZ", "AF");
countryMap.put("MD", "E");
countryMap.put("MN", "AS");
countryMap.put("ME", "E");
countryMap.put("MS", "S");
countryMap.put("MC", "E");
countryMap.put("MM", "AS");
countryMap.put("NA", "AF");
countryMap.put("NR", "O");
countryMap.put("NP", "AS");
countryMap.put("NI", "S");
countryMap.put("NG", "AF");
countryMap.put("NU", "O");
countryMap.put("NE", "AF");
countryMap.put("NO","E");
countryMap.put("NC", "O");
countryMap.put("NZ", "O");
countryMap.put("OM", "AS");
countryMap.put("NL", "E");
countryMap.put("PK", "AS");
countryMap.put("PW", "O");
countryMap.put("PS", "AS");
countryMap.put("PA", "S");
countryMap.put("PG", "O");
countryMap.put("PY", "S");
countryMap.put("PE", "S");
countryMap.put("PF", "O");
countryMap.put("PL", "E");
countryMap.put("PT", "E");
countryMap.put("PR", "S");
countryMap.put("QA", "AS");
countryMap.put("HK", "AS");
countryMap.put("MO", "AS");
countryMap.put("GB", "E");
countryMap.put("CF", "E");
countryMap.put("DO", "S");
countryMap.put("CZ", "E");
countryMap.put("ZA", "AF");
countryMap.put("RO", "E");
countryMap.put("RU", "E");
countryMap.put("RW", "AF");
countryMap.put("UM", "S");
countryMap.put("KN", "S");
countryMap.put("SH", "AF");
countryMap.put("LC", "S");
countryMap.put("MF", "S");
countryMap.put("VC", "S");
countryMap.put("PM", "N");
countryMap.put("WS", "O");
countryMap.put("AS", "O");
countryMap.put("SM", "E");
countryMap.put("EH", "AF");
countryMap.put("ST", "AF");
countryMap.put("SN", "AF");
countryMap.put("SC", "AF");
countryMap.put("RS", "E");
countryMap.put("SL", "AF");
countryMap.put("SG", "AS");
countryMap.put("SR", "S");
countryMap.put("SJ", "E");
countryMap.put("SZ", "AF");
countryMap.put("TJ", "AS");
countryMap.put("TH", "AS");
countryMap.put("TW", "AS");
countryMap.put("TZ", "AF");
countryMap.put("IO", "O");
countryMap.put("TF", "O");
countryMap.put("TL", "AS");
countryMap.put("TG", "AF");
countryMap.put("TK", "O");
countryMap.put("TO", "O");
countryMap.put("TT", "S");
countryMap.put("TN", "AF");
countryMap.put("TM", "AS");
countryMap.put("TR", "AS");
countryMap.put("TV", "O");
countryMap.put("TD", "AF");
countryMap.put("UA", "E");
countryMap.put("UG", "AF");
countryMap.put("AE", "AS");
countryMap.put("UY", "S");
countryMap.put("UZ", "AS");
countryMap.put("VZ", "O");
countryMap.put("VA", "E");
countryMap.put("VE", "S");
countryMap.put("VN", "AS");
countryMap.put("WF", "O");
countryMap.put("CL", "S");
countryMap.put("CN", "AS");
countryMap.put("CY", "E");
countryMap.put("ZM", "AF");
countryMap.put("ZW", "AF");
}
Filtering NSArray of NSDictionaries from values in another NSArray while preserving order
I was trying to avoid this, but so far this is the only way I know how:
NSMutableArray *filteredCountries = [[[NSMutableArray alloc] init] autorelease];
NSArray *countryNames = ... // list of country names. this order matters
NSArray *countries = ... // array of country objects
for (NSString *name in countryNames) {
for (NSDictionary *dict in countries) {
if ([[dict objectForKey:@"name"] isEqualToString:name]) {
[filteredCountries addObject:dict];
}
}
}
It sucks, but it ships. Still welcoming a better, more elegant solution.
[Xcode]How to count NSArray according to type
You can use a dictionary to put objects with the same country in a separate array:
NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];
for (dataclass *location in array) {
if (![dictionary objectForKey:location.country]) {
[dictionary setObject:[NSMutableArray array] forKey:location.country];
}
[(NSMutableArray *)[dictionary objectForKey:location.country] addObject:location];
}
Now, not only you can get the number of different countries like this:
int countriesCount = [dictionary count];
But also, it is easy to populate each section of your table view with locations from the corresponding country.
You can retrieve all the locations for a specific country that has the name countryName
, like this:
NSArray *locationsForCountry = [dictionary objectForKey:countryName];
You can also have the list of all the countries like this:
NSArray *countries = [[dictionary keyEnumerator] allObjects];
How do I get a list of countries in Swift ios?
You can get a list of countries using the NSLocale class's isoCountryCodes
which returns an array of [String]
. From there, you get the country name by using NSLocale
's displayName(forKey:)
method. It looks like this:
var countries: [String] = []
for code in NSLocale.isoCountryCodes {
let id = NSLocale.localeIdentifier(fromComponents: [NSLocale.Key.countryCode.rawValue: code])
let name = NSLocale(localeIdentifier: "en_UK").displayName(forKey: NSLocale.Key.identifier, value: id) ?? "Country not found for code: \(code)"
countries.append(name)
}
print(countries)
Related Topics
How to Tell If Your Code Is Running on an iPhone or an Iphone3G
Get Udid of iOS Device Programmatically
How to Fill a Bezier Path with Gradient Color
Nsurlconnection Deprecated in iOS9
iOS 5 Sdk Is Gone After Upgrade to Xcode 4.5
How to Make My Uibezierpath Animated with Cashapelayer
Install iOS App Directly from Website - No Password, No Itunes, No Udid
Hide Navigationbar When Scrolling Tableview in Collectionview
Detect If a User Has Typed an Emoji Character in Uitextview
Change the Uitextview Text Direction
Memory Leak with Large Core Data Batch Insert in Swift
How to Find the Purple Port for the Front Most Application in iOS 5 and Above
Avqueueplayer Playback Without Gap and Freeze
How to Create and Send the JSON Data to Server Using Swift Language