开发者

How to determine whether class B is a subclass of class A?

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).

My current solutions is to put a method 开发者_JS百科-(void)iAmClassB in there, and perform a respondsToSelector:iAmClassB, but that seems contrived. Am I missing something?


It is available from iOS 2.0 and later version SDK

if ([ClassB isSubclassOfClass:[ClassA class]]) {


    NSLog(@"yes ClassB is SubclassOfClass of ClassA");

}       

Documentation:

isSubclassOfClass:

Returns a Boolean value that indicates whether the receiving class is a subclass of, or identical to, a given class.

   + (BOOL)isSubclassOfClass:(Class)aClass

Parameters

aClass

A class object.

Return Value

YES if the receiving class is a subclass of—or identical to—aClass, otherwise NO.

Availability

Available in iOS 2.0 and later.


id a= ...;
if([a isKindOfClass:[A class]]){
     ...
}

should do the job. You rarely needs to see if it's really a sub class. See here.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜