如何确定B类是否是A类的子类? [英] How to determine whether class B is a subclass of class A?
问题描述
似乎如果您为Mac OS开发, NSObject
具有 isSubclassOfClass
方法。但是当我检查同一类的iOS类引用时,它没有该方法(并且Xcode抱怨该方法)。
It seems that if you develop for Mac OS, NSObject
has the isSubclassOfClass
method. But when I check the iOS class reference for the same class, it does not have the method (and Xcode complains about the method).
我目前的解决方案是在那里放一个方法 - (void)iAmClassB
,并执行 respondsToSelector:iAmClassB
,但这似乎是人为的。我错过了什么吗?
My current solutions is to put a method -(void)iAmClassB
in there, and perform a respondsToSelector:iAmClassB
, but that seems contrived. Am I missing something?
推荐答案
它可从iOS 2.0及更高版本SDK获得
It is available from iOS 2.0 and later version SDK
if ([ClassB isSubclassOfClass:[ClassA class]]) {
NSLog(@"yes ClassB is SubclassOfClass of ClassA");
}
文档:
isSubclassOfClass:
isSubclassOfClass:
返回一个布尔值,指示接收类是否为给定类的
a子类或相同。
Returns a Boolean value that indicates whether the receiving class is a subclass of, or identical to, a given class.
+ (BOOL)isSubclassOfClass:(Class)aClass
参数
aClass
一个类对象。
返回值
YES
如果接收类是 - 的子类相同于 - aClass
,
否则否
。
YES
if the receiving class is a subclass of—or identical to—aClass
,
otherwise NO
.
可用性
在iOS 2.0中可用以后。
Available in iOS 2.0 and later.
这篇关于如何确定B类是否是A类的子类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!