Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Comparing of 'Class' with other 'Class' in Objective-C

Does a comparable function to 'isKindOfClass:' exist for comparing a 'Class' to another (i.e. without constructing an instance of either class). For example, given:

Class class = NSClassFromString(@"NSNumber");

[NSNumber isKindOfClass:class]; // YES
[NSString isKindOfClass:class]; // NO

Thanks!

like image 595
Kevin Sylvestre Avatar asked Aug 12 '10 19:08

Kevin Sylvestre


2 Answers

+ (BOOL)isSubclassOfClass:(Class)aClass

and

Class theClass = NSClassFromString(@"NSNumber");

if ([NSNumber class] == theClass) {
    // YES
}

There is never more than 1 instance of a class, that's why == is the operator you're looking for.

like image 161
Georg Schölly Avatar answered Oct 02 '22 13:10

Georg Schölly


Yeah, you can do:

[NSNumber isSubclassOfClass:class]; //YES
[NSString isSubclassOfClass:class]; //NO

These are class methods on NSObject.

like image 38
Dave DeLong Avatar answered Oct 02 '22 12:10

Dave DeLong