从NSArray创建字母NSDictionary

时间:2016-07-20 13:04:50

标签: ios nsdictionary

假设我有一个如下所示的数组:

NSArray *array = @[@"Bear", @"Black Swan", @"Buffalo", @"Camel", @"Cockatoo", @"Dog", @"Donkey", @"Emu"]

如何创建以下字典呢?

NSDictionary *animals;
animals = @{@"B" : @[@"Bear", @"Black Swan", @"Buffalo"],
            @"C" : @[@"Camel", @"Cockatoo"],
            @"D" : @[@"Dog", @"Donkey"],
            @"E" : @[@"Emu"]};

3 个答案:

答案 0 :(得分:0)

我想你想这样做:

NSMutableDictionary* animals =[NSMutableDictionary new]; NSArray* wAnimals =@[@"Whale", @"Whale Shark", @"Wombat"]; [animals setObject:wAnimals forKey:@"W"];

答案 1 :(得分:0)

如果我正确理解了这个问题,那么你正试图用数组创建一个字典。

试试这个:

NSArray *array = @[@"Bear", @"Black Swan", @"Buffalo", @"Camel", @"Cockatoo", @"Dog", @"Donkey", @"Emu"];

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

for (NSString *animal in array) {
    NSString *firstLetter = [animal substringToIndex:1];

    if (!dictionary[firstLetter]) {
        dictionary[firstLetter] = [[NSMutableArray alloc] init];
    }

    [((NSMutableArray *)dictionary[firstLetter]) addObject:animal];
}

最后,字典的值是:

dictionary = @{@"B" : @[@"Bear", @"Black Swan", @"Buffalo"],
            @"C" : @[@"Camel", @"Cockatoo"],
            @"D" : @[@"Dog", @"Donkey"],
            @"E" : @[@"Emu"]};

答案 2 :(得分:0)

请尝试这个(希望这对你有帮助,我想你想要这样) -

-(NSDictionary*)makeCustomDictionary:(NSString*)yourValue{

    [array addObject:yourValue];

    NSMutableArray *lettersKey = [NSMutableArray array];
    NSMutableArray *values = [NSMutableArray array];

    for (NSString *string in array) {

        if (string.length > 0) {

            NSString *letter = [string substringToIndex:1];

            if (![lettersKey containsObject:letter]) {

                [lettersKey addObject:letter];

                NSString *stringToSearch = letter;

                NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF beginswith[c] %@",stringToSearch]; // if you need case sensitive search avoid '[c]' in the predicate

                NSArray *results = [array filteredArrayUsingPredicate:predicate];

                [values addObject:results];

            }
        }
    }


    return [NSDictionary dictionaryWithObjects:values forKeys:lettersKey];
}
- (IBAction)addNewValue:(id)sender {
    NSDictionary *tempDic=[self makeCustomDictionary:txt.text];
    NSError *error;
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:tempDic
                                                       options:NSJSONWritingPrettyPrinted // Pass 0 if you don't care about the readability of the generated string
                                                         error:&error];
    if (! jsonData) {
        NSLog(@"Got an error: %@", error);
    } else {
        NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
        NSLog(@"dictionary in json form - %@ \n\n or dictionary simple- %@",jsonString,tempDic);
    }

}

此处数组NSMutableArray中的全局viewDidLoad()初始化。 输出 -

 dictionary in json form - 
{
     "D" : ["Dog","Donkey"],
     "B" : ["Bear","Black Swan","Buffalo"],
     "G" : ["Giraffe"],
     "E" : ["Emu"],
     "C" : ["Camel","Cockatoo"]
}

or dictionary simple- {
    B =     (
             Bear,
             "Black Swan",
             Buffalo
             );
    C =     (
             Camel,
             Cockatoo
             );
    D =     (
             Dog,
             Donkey
             );
    E =     (
             Emu
             );
    G =     (
             Giraffe
             );
}