9

I have a NSMutableArray (_theListOfAllQuestions) that I am populating with numbers from a file. Then I compared the objects in that array with qNr (NSString) and I got error. I even casted the array to another NSString, _checkQuestions, just to be sure I am comparing 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:

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'

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?

I could really need some help here to 1) understand the problem, 2)solve the problem?

1
  • Well then _checkQuestions, in at least one case, is an NSNumber and not an NSString. What exactly is in _theListOfAllQuestions? And how is that being populated?
    – Alex Wayne
    Jan 21, 2013 at 19:25

2 Answers 2

15

Replace this line

if ([_checkQuestions isEqualToString:qNr])

with

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

Hope it helps you..

4
  • 2
    There is no need for stringWithFormat as NSNumber already has a property -stringValue that returns the number as a string. Jan 21, 2013 at 19:34
  • @yulz I dont know datatype of _checkQuestions and there is no harm in using stringWithFormat..
    – P.J
    Jan 21, 2013 at 19:36
  • @Praatek, that made it THANKS.
    – PeterK
    Jan 21, 2013 at 19:38
  • Appreciate that @Prateek :-)
    – PeterK
    Jan 21, 2013 at 19:41
2

Your _theListOfAllQuestions array has NSNumber objects and not NSString objects. So you cant use isEqualToString directly.

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++;
 }
6
  • ABC, tried this also earlier and got "No visible @interface for 'NSString' declares the selector 'stringValue'"
    – PeterK
    Jan 21, 2013 at 19:43
  • That is because you should declare _checkQuestions as NSNumber and not NSSString. Your array has NSNumbers and not string. Usage of [NSString stringWithFormat:@"%@",_checkQuestions] is not recommended in this case. That will give unexpected results later.
    – iDev
    Jan 21, 2013 at 19:45
  • 1
    Set _checkQuestions to NSNumber. You can either type cast it [(NSNumber *)_checkQuestions stringValue] in your if statement or change the type where you have declared _checkQuestions to NSNumber Jan 21, 2013 at 19:47
  • I tried both of the above suggestions and get no error but no if-hit either? ...as i get with the suggestion from Prateek.
    – PeterK
    Jan 21, 2013 at 19:51
  • 1
    @ABC, i have continued to test and found a typo i did, which means that your suggestion did work, my embarrassing mistake. You are correct, thanks a lot :-) ...now i learned something very useful! +1
    – PeterK
    Jan 21, 2013 at 19:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.