[英]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
与某些类型进行比较,但对我而言,我看起来比较NSString
和NSString
?
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.