Iphone, Obtaining a List of countries in an NSArray

后端 未结 6 1855
暗喜
暗喜 2020-12-04 09:04

I have a menu that let\'s a user select a country. Exactly like that in the contacts.app country menu within the address field.

Does anyone know a simple way of get

6条回答
  •  一个人的身影
    2020-12-04 09:38

    Thanks chuck.

    If anyone is interested or wanted to find the same solution here is my code for a sorted array of countries.

    Objective-C:

    NSLocale *locale = [NSLocale currentLocale];
    NSArray *countryArray = [NSLocale ISOCountryCodes];
    
    NSMutableArray *sortedCountryArray = [[NSMutableArray alloc] init];
    
    for (NSString *countryCode in countryArray) {
    
        NSString *displayNameString = [locale displayNameForKey:NSLocaleCountryCode value:countryCode];
        [sortedCountryArray addObject:displayNameString];
    
    }
    
    [sortedCountryArray sortUsingSelector:@selector(localizedCompare:)];
    

    Swift:

    let locale = NSLocale.currentLocale()
    let countryArray = NSLocale.ISOCountryCodes()
    var unsortedCountryArray:[String] = []
    for countryCode in countryArray {
        let displayNameString = locale.displayNameForKey(NSLocaleCountryCode, value: countryCode)
        if displayNameString != nil {
            unsortedCountryArray.append(displayNameString!)
        }
    }
    let sortedCountryArray = sorted(unsortedCountryArray, <)
    

    Swift 3

        let locale = NSLocale.current
        let unsortedCountries = NSLocale.isoCountryCodes.map { locale.localizedString(forRegionCode: $0)! }
        let sortedCountries = unsortedCountries.sorted()
    

提交回复
热议问题