简体   繁体   中英

Strange `isKindOfClass` behaviour

With reference to the code below, I have found that isKindOfClass is failing to identify a class.

id parent = [self.tableDataSource objectAtIndex:indexPath.row];
//parent Can be either 'document' or 'folder'


    NSLog(@"'%@'='%@' ?",[parent class],[Document class]);  
BOOL classCheck = [[parent class] isKindOfClass:[Document class]];  
NSLog (@"%@", classCheck? @"Yes!" : @"No!");

Console Log:

2011-01-20 10:44:29.746 ApplicationName[906:307] 'Document'='Document' ?
2011-01-20 10:44:29.756 ApplicationName[906:307]No!

Has anyone came across this before?

[parent isKindOfClass:[Document class]] should return YES. [parent class] is a class object which is most likely not an instance of class Document.

Here is why NSLog() prints class names (from the docs):

+ (NSString *) description

NSObject's implementation of this method simply prints the name of the class.

You should be writing: [ parent isKindOfClass: [ Document class ] ]

[ parent class ] will return a Class object and a Class object is not a kind of Document.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM