iosobjective-cnsdictionaryunrecognized-selector

SortedArrayUsingSelector unrecognized Selector


i am new to ios and i am really stuck at finding solution for this...

    animals = @{@"B" : @[@"Bear", @"Black Swan", @"Buffalo"],
            @"C" : @[@"Camel", @"Cockatoo"],
            @"D" : @[@"Dog", @"Donkey"],
            @"E" : @[@"Emu"],
            @"G" : @[@"Giraffe", @"Greater Rhea"],
            @"H" : @[@"Hippopotamus", @"Horse"],
            @"K" : @[@"Koala"],
            @"L" : @[@"Lion", @"Llama"],
            @"M" : @[@"Manatus", @"Meerkat"],
            @"P" : @[@"Panda", @"Peacock", @"Pig", @"Platypus", @"Polar Bear"],
            @"R" : @[@"Rhinoceros"],
            @"S" : @[@"Seagull"],
            @"T" : @[@"Tasmania Devil"],
            @"W" : @[@"Whale", @"Whale Shark", @"Wombat"]};
    animalSectionTitles = [[animals allKeys] sortedArrayUsingSelector:@selector(compare:)];

the code above doesn't error but when i trying to insert array into NSDictionary, it always shows unrecognized selecter. What should i do to solve this problem?

while (sqlite3_step(statement)==SQLITE_ROW) {
        Word *univer = [[Word alloc] init];
        univer.Id =[NSString stringWithUTF8String:(char *) sqlite3_column_text(statement,0)];
        univer.word=[NSString stringWithUTF8String:(char *) sqlite3_column_text(statement,1)];
        //NSLog(@"%@",univer.word);
        NSString *first = [univer.word substringToIndex:1];
        NSLog(@"%@",first);
        if([first isEqualToString:@"A"] || [first isEqualToString:@"a"]){
            [_A addObject:univer.word];
        }
        else if([first isEqualToString:@"B"] || [first isEqualToString:@"b"]){
            [_B addObject:univer.word];
        }
        else if([first isEqualToString:@"C"] || [first isEqualToString:@"c"]){
            [_C addObject:univer.word];
        }
        else if([first isEqualToString:@"D"] || [first isEqualToString:@"d"]){
            [_D addObject:univer.word];
        }
        else if([first isEqualToString:@"E"] || [first isEqualToString:@"e"]){
            [_E addObject:univer.word];
        }
        else if([first isEqualToString:@"F"] || [first isEqualToString:@"f"]){
            [_F addObject:univer.word];
        }
        else if([first isEqualToString:@"G"] || [first isEqualToString:@"g"]){
            [_G addObject:univer.word];
        }
        else if([first isEqualToString:@"H"] || [first isEqualToString:@"h"]){
            [_H addObject:univer.word];
        }
    }
animals= [NSDictionary dictionaryWithObjectsAndKeys:_A,_B,_C,_D,_E,_F,_G,_H, nil];
animalSectionTitles = [[animals allKeys] sortedArrayUsingSelector:@selector(compare:)];

Solution

  • The problem is this line:

    animals= [NSDictionary dictionaryWithObjectsAndKeys:_A,_B,_C,_D,_E,_F,_G,_H, nil];
    

    When you create a dictionary with dictionaryWithObjectsAndKeys: the list of items you supply have to be objectA, keyA, objectB, keyB, objectC, keyC, nil. Right now it looks like all you have is objectA, objectB, objectC, nil. Which looks like this if you write it out:

    animals = @{@[@"Bear", @"Black Swan", @"Buffalo"] : @[@"Camel", @"Cockatoo"],
                @[@"Dog", @"Donkey"] : @[@"Emu"],
                @[@"Giraffe", @"Greater Rhea"] : @[@"Hippopotamus", @"Horse"]};
    

    When you call [[animals allKeys] sortedArrayUsingSelector:@selector(compare:)] it's calling the compare: selector on all of the items in [animals allKeys] which are NSArrays which don't support the compare: method and thus the unrecognized selector error.

    Not sure exactly what you want but try this:

    animals= [NSDictionary dictionaryWithObjectsAndKeys:_A, @"A", _B, @"B", _C, @"C", nil];