简体   繁体   English

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

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

I have a NSMutableArray (_theListOfAllQuestions) that I am populating with numbers from a file. 我有一个NSMutableArray (_theListOfAllQuestions) ,我用文件中的数字填充。 Then I compared the objects in that array with qNr (NSString) and I got error. 然后我用qNr (NSString)比较了该数组中的对象,我得到了错误。 I even casted the array to another NSString , _checkQuestions , just to be sure I am comparing NSStrings . 我甚至将数组转换为另一个NSString_checkQuestions ,以确保我正在比较NSStrings I tested using item to compare also. 我测试使用项目进行比较。

-(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++;
 }

When running this code i get the following NSLog : 运行此代码时,我得到以下NSLog

read_A_Question: 421
item: 1193
_checkQuestions: 1193

...and error: ......和错误:

-[__NSCFNumber isEqualToString:]: unrecognized selector sent to instance 0x9246d80 *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFNumber isEqualToString:]: unrecognized selector sent to instance 0x9246d80' - [__ NSCFNumber isEqualToString:]:无法识别的选择器发送到实例0x9246d80 ***由于未捕获的异常'NSInvalidArgumentException'终止应用程序,原因:' - [__ NSCFNumber isEqualToString:]:无法识别的选择器发送到实例0x9246d80'

I do believe that I still comparing NSString with a number of some sort but to me it looks like I am comparing NSString vs. NSString ? 我相信我仍然将NSString与某些类型进行比较,但对我而言,我看起来比较NSStringNSString

I could really need some help here to 1) understand the problem, 2)solve the problem? 我真的需要一些帮助,1)了解问题,2)解决问题?

Replace this line 替换此行

if ([_checkQuestions isEqualToString:qNr])

with

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

Hope it helps you.. 希望它可以帮助你..

Your _theListOfAllQuestions array has NSNumber objects and not NSString objects. 您的_theListOfAllQuestions数组具有NSNumber对象,而不是NSString对象。 So you cant use isEqualToString directly. 所以你不能直接使用isEqualToString

Try this, 试试这个,

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.

相关问题 比较NSUpdatedObjectsKey和NSString我得到错误: - [__ NSSetI isEqualToString:]:发送到实例的无法识别的选择器 - comparing NSUpdatedObjectsKey to NSString I get error: -[__NSSetI isEqualToString:]: unrecognized selector sent to instance [NSCFNumber isEqualToString:]:发送到实例的无法识别的选择器 - [NSCFNumber isEqualToString:]: unrecognized selector sent to instance NSString isEqualToString-无法识别的选择器发送到实例 - NSString isEqualToString - Unrecognized selector sent to instance 我得到__NSCFNumber isEqualToString:]错误 - I get a __NSCFNumber isEqualToString:] error 目标C错误[__NSCFNumber长度]:无法识别的选择器已发送到实例 - Objective C error [__NSCFNumber length]: unrecognized selector sent to instance UILocalizedIndexedCollat​​ion-[__ NSCFNumber length]:无法识别的选择器已发送到实例 - UILocalizedIndexedCollation -[__NSCFNumber length]: unrecognized selector sent to instance ios [__NSCFNumber length]:无法识别的选择器已发送到实例 - ios [__NSCFNumber length]: unrecognized selector sent to instance -[__ NSCFNumber行]:无法识别的选择器已发送到实例 - -[__NSCFNumber row]: unrecognized selector sent to instance [__NSCFNumber长度]:无法识别的选择器已发送到实例 - [__NSCFNumber length]: unrecognized selector sent to instance [__NSCFNumber length]:无法识别的选择器发送到实例UITableView - [__NSCFNumber length]: unrecognized selector sent to instance UITableView
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM