isKindOfClass 当子类是该类的派生类或者父类时, 返回值是YES;
isMemberOfClass 只有当子类是该类的类型时,返回YES
UILabel *label = [[UILabelnew]autorelease];
if ([label isKindOfClass:[UILabelclass]]) {
NSLog(@" is KindOfClass? YES!");
}
if ([label isKindOfClass:[NSObjectclass]]) {
NSLog(@" is KindOfClass? YES!");
}
if ([label isMemberOfClass:[UILabel class]]) {
NSLog(@" is MemberOfClass? YES!");
}
if ([label isMemberOfClass:[NSObject class]]) {
NSLog(@" is MemberOfClass? NO!");
}
//运行结果如下:
2015-10-10 10:18:50.425 KindClass[1827:73759] is KindOfClass? YES!
2015-10-10 10:18:50.426 KindClass[1827:73759] is KindOfClass? YES!
2015-10-10 10:18:50.426 KindClass[1827:73759] is MemberOfClass? YES!
2015-10-10 10:18:50.426 KindClass[1827:73759] is MemberOfClass? NO!
//但是当子类对象是NS;类型时 isKindOfClass不受影响,而isMemberOfClass返回值一直是NO
NSMutableArray *array = [[[NSMutableArrayalloc]init]autorelease];
if ([array isKindOfClass:[NSArrayclass]]) {
NSLog(@" is KindOfClass? YES!");
}
if ([array isKindOfClass:[NSObjectclass]]) {
NSLog(@" is KindOfClass? YES!");
}
if ([array isMemberOfClass:[NSArray class]]) {
NSLog(@" is MemberOfClass? YES!");
}
if (![array isMemberOfClass:[NSObjectclass]]) {
NSLog(@" is MemberOfClass? NO!");
}
//运行结果如下:
2015-10-10 10:35:48.262 KindClass[1906:79483] is KindOfClass? YES!
2015-10-10 10:35:48.263 KindClass[1906:79483] is KindOfClass? YES!
2015-10-10 10:35:48.263 KindClass[1906:79483] is MemberOfClass? NO!