问题描述:

I have a array like (ann bork,bill john,born all, Catire all ..... So on)

I want like dictionary (keys as letters and values as names)

A = Ann Bork

B = bill john

born all

C = Catire all

....

Z = Zebra cross

Help me pls Thanx

网友答案:

Assuming that you want an array of dictionaries where the key is the first letter of the string value. Here is what I would do.

-(NSArray *)getArrayOfDictionaries:(NSArray *)array {
    NSMutableArray *arrayOfDictionaries = [NSMutableArray new];
    for (NSString *nameString in array) {
        NSString *keyString = [[nameString substringToIndex:1] uppercaseString];
        NSDictionary *nameDictionary = [[NSDictionary alloc] initWithObjectsAndKeys:nameString, keyString, nil];
        [arrayOfDictionaries addObject:nameDictionary];
    }
    return arrayOfDictionaries;
}

This function takes an array of strings and returns an array of dictionaries.

相关阅读:
Top