Search code examples
iosobjective-cunrecognized-selector

[__NSCFBoolean count]: unrecognized selector : Best way to prevent this crash


I am getting this error on the conditional if statement. Is there a good way to prevent this error from showing up? Any tips or suggestions are appreciated. I am guessing subanswer for some reason is a boolean.

id subAnswer = [answer objectForKey:@"answer"];
NSArray *subAnswerKeyList;
if (subAnswer != [NSNull null] && subAnswer != nil && [subAnswer count] > 0 ) {
    ...
}

Solution

  • Replace your if statement with:

    if ([subAnswer isKindOfClass:[NSArray class]] && [subAnswer count]) {
    }
    

    Your subAnswer is actually a number representing a BOOL value. You need to see why you expect it to be an array.