判断对象类型
-(BOOL) isKindOfClass: classObj判断是否是这个类或者这个类的子类的实例
-(BOOL) isMemberOfClass: classObj 判断是否是这个类的实例
isKindOfClass和isMemberOfClass 的区别//转自
判断对象类型
-(BOOL) isKindOfClass: classObj判断是否是这个类或者这个类的子类的实例
-(BOOL) isMemberOfClass: classObj 判断是否是这个类的实例
测试实例:
#import <Foundation/Foundation.h> @interface Person : NSObject { NSString *name; } -(void)setName:(NSString*)n; @end ///////// #import "Person.h" @implementation Person -(void)setName:(NSString *)n { name = n; } @end /////////// #import "Person.h" @interface Teacher : Person -(void)teach; @end /////////////// #import "Teacher.h" @implementation Teacher -(void)teach { NSLog(@"我教数学"); } @end //////////// Person *person = [[Person alloc] init]; Teacher *teacher = [[Teacher alloc] init]; //YES if ([teacher isMemberOfClass:[Teacher class]]) { NSLog(@"teacher Teacher类的成员"); } //NO if ([teacher isMemberOfClass:[Person class]]) { NSLog(@"teacher Person类的成员"); } //NO if ([teacher isMemberOfClass:[NSObject class]]) { NSLog(@"teacher NSObject类的成员"); } //////////// Person *person = [[Person alloc] init]; Teacher *teacher = [[Teacher alloc] init]; //YES if ([teacher isKindOfClass:[Teacher class]]) { NSLog(@"teacher 是 Teacher类或Teacher的子类"); } //YES if ([teacher isKindOfClass:[Person class]]) { NSLog(@"teacher 是 Person类或Person的子类"); } //YES if ([teacher isKindOfClass:[NSObject class]]) { NSLog(@"teacher 是 NSObject类或NSObject的子类"); }