I encountered some code that might potentially use Nil
as the class in an isKindOfClass:
check and was curious what would happen in that case. Using Xcode 6.2, with the OS X 10.10 SDK and running on 10.9.5:
for (id obj in @[
@"foo",
@"This is a long string.",
[NSString new],
[NSString stringWithUTF8String:"Hello, world!"],
[@"This is a constructed " stringByAppendingString:@"string."],
@YES,
@2015,
@[],
@[ @42, ],
@[ @"hello", ],
@{ @"hi": @5, },
]) {
NSLog(@"[obj className] => %@; [obj isKindOfClass:Nil] => %@", [obj className], [obj isKindOfClass:Nil] ? @"YES" : @"NO");
}
produces:
[obj className] => __NSCFConstantString; [obj isKindOfClass:Nil] => YES
[obj className] => __NSCFConstantString; [obj isKindOfClass:Nil] => YES
[obj className] => __NSCFConstantString; [obj isKindOfClass:Nil] => YES
[obj className] => __NSCFString; [obj isKindOfClass:Nil] => NO
[obj className] => __NSCFString; [obj isKindOfClass:Nil] => NO
[obj className] => __NSCFBoolean; [obj isKindOfClass:Nil] => NO
[obj className] => __NSCFNumber; [obj isKindOfClass:Nil] => NO
[obj className] => __NSArrayI; [obj isKindOfClass:Nil] => NO
[obj className] => __NSArrayI; [obj isKindOfClass:Nil] => NO
[obj className] => __NSArrayI; [obj isKindOfClass:Nil] => NO
[obj className] => __NSDictionaryI; [obj isKindOfClass:Nil] => NO
Why does [obj isKindOfClass:Nil]
return YES
for __NSCFConstantString
objects and nothing else (or, at least, nothing else I've tried)?
edit: Ultimately, the solution in the code I'm working on is to prevent the call from happening when the class is Nil
, so this question is really just a curiosity, especially since the behavior is different on iOS 9.1 and OS X 10.11.1 (per the comments).