isKindOfClass和isMemberOfClass的区别
isKindOfClass :Returns a Boolean value that indicates whether the receiver is an instance of given class or an instance of any class that inherits from that class.
返回BOOL值,指定该对象是不是给定类的实例或者给定类的子类的实例。
isMemberOfClass:Returns a Boolean value that indicates whether the receiver is an instance of a given class.
返回BOOL值,指定该对象是不是给定类的实例。
如,Professor : Teacher : Person : NSObject
Teacher*teacher = [Teacher new];
NSLog(@"Person1:%zd", [teacher isKindOfClass:[Teacher class]]);
NSLog(@"Person2:%zd", [teacher isKindOfClass:[Person class]]);
NSLog(@"Person3:%zd", [teacher isMemberOfClass:[Teacher class]]);
NSLog(@"Person4:%zd", [teacher isMemberOfClass:[Person class]]);
NSLog(@"Person44:%zd", [teacher isKindOfClass:[Professor class]]);
NSLog(@"Person45:%zd", [teacher isMemberOfClass:[Professor class]]);
NSLog(@"Person5:%zd", [[teacher class]isKindOfClass:[Teacher class]]);
NSLog(@"Person6:%zd", [[teacher class]isKindOfClass:[Person class]]);
NSLog(@"Person7:%zd", [[teacher class]isMemberOfClass:[Teacher class]]);
NSLog(@"Person8:%zd", [[teacher class]isMemberOfClass:[Person class]]);
输出:
2017-08-26 23:42:49.076 testAPP[9151:934106] Person1:1
2017-08-26 23:42:49.076 testAPP[9151:934106] Person2:1
2017-08-26 23:42:49.077 testAPP[9151:934106] Person3:1
2017-08-26 23:42:49.077 testAPP[9151:934106] Person4:0
2017-08-26 23:42:49.077 testAPP[9151:934106] Person44:0
2017-08-26 23:42:49.077 testAPP[9151:934106] Person45:0
2017-08-26 23:42:49.077 testAPP[9151:934106] Person5:0
2017-08-26 23:42:49.077 testAPP[9151:934106] Person6:0
2017-08-26 23:42:49.077 testAPP[9151:934106] Person7:0
2017-08-26 23:42:49.077 testAPP[9151:934106] Person8:0
其中,5-8中,[teacher class]的返回值为 Teacher,它肯定不是任何一个类的实例,因为他是“类”呀!
美团面试中问到了。