繁体   English   中英

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

[英]Why do i get an error comparing NSString's? (-[__NSCFNumber isEqualToString:]: unrecognized selector sent to instance)

我有一个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)解决问题?

替换此行

if ([_checkQuestions isEqualToString:qNr])

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

希望它可以帮助你..

您的_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++;
 }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM