SortedArrayUsingSelector无法识别的选择器

时间:2014-11-23 17:02:59

标签: ios objective-c nsdictionary unrecognized-selector

我是ios的新手,我真的很难找到解决方案......

    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:)];

上面的代码没有错误但是当我试图将数组插入NSDictionary时,它总是显示无法识别的选择。我该怎么做才能解决这个问题?

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:)];

1 个答案:

答案 0 :(得分:2)

问题在于这一行:

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

使用dictionaryWithObjectsAndKeys:创建字典时,您提供的项目列表必须为objectA, keyA, objectB, keyB, objectC, keyC, nil。现在看起来你只有objectA, objectB, objectC, nil。如果你把它写出来,它看起来像这样:

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

当您致电[[animals allKeys] sortedArrayUsingSelector:@selector(compare:)]时,它正在调用compare:[animals allKeys]NSArraycompare:选项卡上的animals= [NSDictionary dictionaryWithObjectsAndKeys:_A, @"A", _B, @"B", _C, @"C", nil]; 选项卡,这些项目{{1}}不支持{{1}}方法,因此无法识别的选择器错误。

不确定你想要什么,但试试这个:

{{1}}