简体   繁体   中英

Sort [NSDictionary allKeys] alphabetically

I'm developing an iOS 5.0+ app with latest SDK.

I want to show a UIPickerView with a list of countries. These countries will be sorted alphabetically. And when the user select a country, I have to stored its ISO Code.

This is the code I'm using now to get countries' names localized:

+ (NSArray*)countriesNames
{
    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:)];

    return sortedCountryArray;
}

But, I need to use something, for example, a NSDictionary ; that let me know get ISO Code and localized name.

I have tried to use a NSDictionary instead of a NSArray :

+ (NSDictionary*)countriesNames
{
    NSLocale *locale = [NSLocale currentLocale];
    NSArray *countryArray = [NSLocale ISOCountryCodes];

    NSMutableDictionary* sortedCountryDic = [[NSMutableDictionary alloc] init];

    for (NSString *countryCode in countryArray)
    {
        NSString* displayNameString = [locale displayNameForKey:NSLocaleCountryCode
                                                          value:countryCode];
        [sortedCountryDic setObject:countryCode forKey:displayNameString];

    }

    [[sortedCountryDic allKeys] sortUsingSelector:@selector(localizedCompare:)];

    return sortedCountryDic;
}

But I get a compile time exception: No visible @interface for 'NSArray' declares the selector 'sortUsingSelector:' .

Here:

[[sortedCountryDic allKeys] sortUsingSelector:@selector(localizedCompare:)];

How can I sort allKeys?
Is there any way to get ISOCode using its displayName?

sortUsingSelector: is a method of NSMutableArray not a method of NSArray you can use sortedArrayUsingSelector to sort the array, and you will get a new array with sorted contents

- (NSArray *)sortedArrayUsingSelector:(SEL)comparator


+ (NSArray*)countriesNames
{
    NSLocale *locale = [NSLocale currentLocale];
    NSArray *countryArray = [NSLocale ISOCountryCodes];

    NSMutableDictionary* sortedCountryDic = [[NSMutableDictionary alloc] init];

    for (NSString *countryCode in countryArray)
    {
        NSString* displayNameString = [locale displayNameForKey:NSLocaleCountryCode
                                                          value:countryCode];
        [sortedCountryDic setObject:countryCode forKey:displayNameString];

    }

    return [[sortedCountryDic allKeys] sortedArrayUsingSelector:@selector(localizedCompare:)];    
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM