Search code examples
objective-cintrospection

-isKindOfClass: not working with NSCFString


Why isn't my if-statement working? It doesn't accept NSCFStrings... Basically I'm doing this:

if ([myObj isKindOfClass:[NSString class]]) {
    // ...
}
else if ([myObj isKindOfClass:[NSNumber class]]) {
    //...
}
else {
    NSLog(@"Invalid NSMutableArray.\n\tWrong type in array: %@, The value is: %@.",[myObj class], myObj);
}

This is inside a loop where I am iterating over an NSMutableArray, with only NSNumbers and NSStrings inside.
Strange enough, it accepts NSNumbers, but no NSStrings!
The output is always:

Invalid NSMutableArray.
    Wrong type in array: NSCFString, The value is: [Value I put in the string].

I thought NSCFString was a subclass of NSString and -isKindOfClass: detected subclasses too?

UPDATE:
I solved it myself. It was indeed caused by –– as stated by @jjv360 –– the missing else before the second if. I thought that was a typo, because I retyped it here instead of copy-pasting, that because I nested a few more if-elses and things got cluttered (that's a google translate-translation).
Pretty stupid after all.

jjv360 Please post this as an answer.


Solution

  • Here's my comment posted as an answer:

    There is a missing else statement between your if statements. Without it, it will run the code in the first block as well as the code in the else block at the end...