[NSNull isEqualToString:]: unrecognized selector sent to instance
try to compare a field returned by web-service and which contains only either the String true
or false
(YES, it's STRING and not boolean), so i try to compare it with another string like this :
if ([withOptions isEqualToString:@"true"]) {
annotation.stationLavage=@"with";
}else {
开发者_JS百科 annotation.stationLavage=@"without";
}
so when withOptions
string contains the "true" string, all is ok, and when it contains the "false" string i got this exception in the log :
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSNull isEqualToString:]: unrecognized selector sent to instance 0x2984d68'
i am pretty sure that in all cases, withOptions
contains a string value (either "true" or "false") and it never contains NULL.
i am pretty sure that in all cases, withOptions contains a string value (either "true" or "false") and it never contains NULL.
Clearly, that assumption is wrong. :)
On the line before that if()
statement, add NSLog(@"%@ - %@", withOptions, [withOptions class]);
Not that NSNull
and NULL
are not the same thing; NSNull
is a class whose singleton instance represents "no value" in containers (and other things) that don't accept nil
as values.
When that crash occurs, withOptions
is referring to an instance of NSNull
.
This is my code, easy for understand.
if ([[info objectForKey:@"address"] isKindOfClass:[NSNull class]]) {
dto.address = @"Unknown";
}
If you want to do a safe comparaison and your are not 100% that your "withOption" contains a string , because it could be null for instance (the case where no option has been given) you could do this:
if ([@"true" isEqualToString:withAnnotation]) {
annotation.stationLavage=@"with";
}else {
annotation.stationLavage=@"without";
}
Note that you can even shorten the comparaison:
annotation.stationLavage=[@"true" isEqualToString:withAnnotation]?@"with":@"without";
精彩评论