I have an interesting problem where I am trying to call class methods on an class which I essentially know nothing about in my test method. I can inspect its inheritance and any protocols it may implement but can’t see an easy way to just call a method on it without getting tied up with an NSInvocation. The code below, albeit crudely, tries to demonstrate the problem I am having.
@interface ClassA : NSObject
+ (Class)classIsPartialClassOf;
@end
@implementation ClassA
+ (Class)classIsPartialClassOf {
return [NSString class];
}
@end
@interface ClassB : NSObject
@end
@implementation ClassB
- (id)init {
[ClassB testClass:[ClassA class]];
}
+ (void)testClass:(Class)classDecl {
/* obviously if you know the type you can just call the method */
[ClassA classIsPartialClassOf];
/* but in my instance I do not know the type, obviously there are no classmethods to perform selector such as the fictional one below */
[classDecl performSelector:@selector(classIsPartialClassOf)];
}
@end
Methods for getting implementations seem to return instance variants and I can’t get them to fire on the static class itself.
Are my options limited to invocations or have I missed something obvious and should kick myself?
Thank you in advance for your help.
Then use
objc_getMetaClass("ClassName")instead ofobjc_getClass. Class objects are objects themselves and are instances of their metaclass. If you pass the metaclass object to e. g.class_getMethod(), everything will be fine.