开发者

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

开发者 https://www.devze.com 2023-01-11 13:52 出处:网络
Does 开发者_Go百科a comparable function to \'isKindOfClass:\' exist for comparing a \'Class\' to another (i.e. without constructing an instance of either class). For example, given:

Does 开发者_Go百科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!


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


Yeah, you can do:

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

These are class methods on NSObject.

0

精彩评论

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