Why isKindOfClass: Nil returns YES on __NSCFConstantString?

I came across some code that could potentially be used Nilas a class in validation isKindOfClass:, and was curious what would happen in this case. Using Xcode 6.2, with the OS X 10.10 SDK and runs 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");
}

gives:

[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 it [obj isKindOfClass:Nil]return YESfor objects __NSCFConstantStringand nothing more (or, at least, haven’t tried anything else)?

edit: Ultimately, the solution in the code I'm working on is to prevent the call when the class is Nil, so this question is really just curiosity, especially since the behavior is different from iOS 9.1 and OS X 10.11.1 (for comments).

+4

Source: https://habr.com/ru/post/1612946/


All Articles