转载于:http://blog.youkuaiyun.com/onlyou930/article/details/7306148
isMemberOfClass只能用来判断前者,不能用来判断后者。
可以说:isMemberOfClass不能检测任何的类都是基于NSObject类这一事实,而isKindOfClass可以。
以下为自己总结:
[Kitty class] ---用来获取对象的类
@interface SonKitty : Kitty
@property (nonatomic, assign) int age;
@end
...
//子类的对象调用子类方法的实现
SonKitty *son = [[SonKitty alloc] init];
[son print];
UIView *v = [[UIView alloc] init];
NSLog(@"%@", v);
...
//isKindOfClass匹配子类和父类的对象
//isMemberOfClass严格匹配类型
if (![v isKindOfClass:[Kitty class]]) {
NSLog(@"v is not a kind of Kitty");
}
else {
NSLog(@"v is a kind of Kitty");
}
if (![v isMemberOfClass:[Kitty class]]) {
NSLog(@"v is not a member of Kitty");
}
else {
NSLog(@"v is a member of Kitty");
}
if (![son isKindOfClass:[Kitty class]]) {
NSLog(@"son is not a kind of Kitty");
}
else {
NSLog(@"son is a kind of Kitty");
}
if (![son isMemberOfClass:[Kitty class]]) {
NSLog(@"son is not a member of Kitty");
}
else {
NSLog(@"son is a member of Kitty");
}
输出结果:
2014-08-04 22:22:28.929 NSObjectApp2[35028:1575681] v is not a kind of Kitty
2014-08-04 22:22:28.930 NSObjectApp2[35028:1575681] v is not a member of Kitty
2014-08-04 22:22:28.930 NSObjectApp2[35028:1575681] son is a kind of Kitty
2014-08-04 22:22:28.931 NSObjectApp2[35028:1575681] son is not a member of Kitty
2,
//本质上,用%@打印对象的时候,打印的是该对象调用description方法所返回的字符串
NSLog(@"%@:%@", [son description], son);
2014-08-04 22:22:28.927 NSObjectApp2[35028:1575681] <SonKitty: 0xae0af60> 这里是SonKitty: 0!:<SonKitty: 0xae0af60> 这里是SonKitty: 0!
可以看出输出结果是一样的。