views:

80

answers:

2

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!

+6  A: 

+ (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.

Georg
Hi Georg. The Second example results in a compile error "Expected expression before 'NSNumber'", however the first one works! Do you know if it is possible to limit to not include subclasses? Thanks!
Kevin Sylvestre
@Georg the if statement should be `if ([NSNumber class] == class)`. For more info, see: http://stackoverflow.com/questions/3107213
Dave DeLong
@Kevin: Fixed that, I should have checked it before. Sorry.
Georg
@Dave: Thanks, that other question is really interesting.
Georg
+4  A: 

Yeah, you can do:

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

These are class methods on NSObject.

Dave DeLong
Thanks Dave. Is it possible to limit to not include subclasses? Thanks.
Kevin Sylvestre
@Kevin as @Georg points out, you can do: `[NSNumber class] == class`
Dave DeLong
Ah cool! Thanks Dave!
Kevin Sylvestre