Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 -(void)iAmClassB in there, and perform a respondsToSelector:iAmClassB, but that seems contrived. Am I missing something?

like image 695
Joris Weimar Avatar asked Sep 02 '11 10:09

Joris Weimar


People also ask

How do you know if a class B is subclass of class A?

issubclass() in Python This function returns True if the given class is the subclass of given class else it returns False. Parameters: Object: class to be checked. classinfo: class, types or a tuple of classes and types.

What does it mean to say the class A is a proper subclass of the class B?

That is, given classes A and B, A is a subclass of B if and only if every member of A is also a member of B. If A and B are sets, then of course A is also a subset of B.


1 Answers

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.

like image 160
Vijay-Apple-Dev.blogspot.com Avatar answered Oct 08 '22 18:10

Vijay-Apple-Dev.blogspot.com