首页 文章

为什么我会比较NSString的错误? ( - [__ NSCFNumber isEqualToString:]:发送到实例的无法识别的选择器)

提问于
浏览
9

我有一个 NSMutableArray (_theListOfAllQuestions) ,我正在填充文件中的数字 . 然后我将该数组中的对象与 qNr (NSString) 进行了比较,我得到了错误 . 我甚至将数组转换为另一个 NSString_checkQuestions ,以确保我正在比较 NSStrings . 我测试使用项目进行比较 .

-(void)read_A_Question:(NSString *)qNr {
NSLog(@"read_A_Question: %@", qNr);
int counter = 0;
for (NSString *item in _theListOfAllQuestions) {
    NSLog(@"item: %@", item);
    _checkQuestions = _theListOfAllQuestions[counter]; //_checkQuestion = NSString
    NSLog(@"_checkQuestions: %@", _checkQuestions);
    if ([_checkQuestions isEqualToString:qNr]) {
        NSLog(@">>HIT<<");
        exit(0);   //Just for the testing
    }
    counter++;
 }

运行此代码时,我得到以下 NSLog

read_A_Question: 421
item: 1193
_checkQuestions: 1193

......和错误:

  • [__ NSCFNumber isEqualToString:]:无法识别的选择器发送到实例0x9246d80 ***由于未捕获的异常'NSInvalidArgumentException'终止应用程序,原因:' - [__ NSCFNumber isEqualToString:]:无法识别的选择器发送到实例0x9246d80'

我确实认为我仍然将 NSString 与某些类型进行比较,但对我而言,我似乎在比较 NSStringNSString

我真的需要一些帮助,1)了解问题,2)解决问题?

2 回答

  • 15

    替换此行

    if ([_checkQuestions isEqualToString:qNr])
    

    if ([[NSString stringWithFormat:@"%@",_checkQuestions] isEqualToString:[NSString stringWithFormat:@"%@",qNr]])
    

    希望它可以帮助你..

  • 2

    您的 _theListOfAllQuestions 数组具有 NSNumber 个对象而不是 NSString 个对象 . 所以你不能直接使用 isEqualToString .

    试试这个,

    for (NSString *item in _theListOfAllQuestions) {
        NSLog(@"item: %@", item);
        _checkQuestions = _theListOfAllQuestions[counter]; //_checkQuestion = NSString
        NSLog(@"_checkQuestions: %@", _checkQuestions);
        if ([[_checkQuestions stringValue] isEqualToString:qNr]) {
            NSLog(@">>HIT<<");
            exit(0);   //Just for the testing
        }
        counter++;
     }
    

相关问题