gpt4 book ai didi

ios - SortedArrayUsingSelector 无法识别的选择器

转载 作者:行者123 更新时间:2023-11-28 21:54:24 24 4
gpt4 key购买 nike

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

最佳答案

问题是这一行:

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:)] 时,它会在 [ animals allKeys]NSArray,不支持 compare: 方法,因此出现无法识别的选择器错误。

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

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

关于ios - SortedArrayUsingSelector 无法识别的选择器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27091698/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com