Question

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.

Was it helpful?

Solution

"Methods for getting implementations seem to return instance variants and I can't get them to fire on the static class itself."

Then use objc_getMetaClass("ClassName") instead of objc_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.

OTHER TIPS

What is the problem? Your code

[classDecl performSelector:@selector(classIsPartialClassOf)];

should work. As will (simpler to write)

[classDecl classIsPartialClassOf];

Class objects are objects. And class methods are simply methods called on a class object.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top