如何获取类方法的选择器?

时间:2015-02-21 15:57:21

标签: ios objective-c macos selector objective-c-runtime

我有下一个代码,我得到一个指向实例方法的指针:

#import <Foundation/Foundation.h>
#import <objc/runtime.h>

@interface TestClass : NSObject
@end

@implementation TestClass
- (void)someMethod { // This is instance method, it's okay
  NSLog(@"Hello from some method!");
}
@end

int main(int argc, const char * argv[]) {
  typedef void (*MethodWithoutParams)();
  MethodWithoutParams someMethodImplementation = 
class_getMethodImplementation([TestClass class], @selector(someMethod));
  someMethodImplementation();
  return 0;
}

效果很好。但如果我想获得一个指向类方法的指针,它就不起作用了:

#import <Foundation/Foundation.h>
#import <objc/runtime.h>

@interface TestClass : NSObject
@end

@implementation TestClass
+ (void)someMethod { // This is class method, it doesn't work
  NSLog(@"Hello from some method!");
}
@end

int main(int argc, const char * argv[]) {
  typedef void (*MethodWithoutParams)();
  MethodWithoutParams someMethodImplementation = 
class_getMethodImplementation([TestClass class], @selector(someMethod));
  someMethodImplementation();
  return 0;
}

它无法正常工作,因为它无法查找方法实现。

我确信必须有效,因为如果我以这种方式获得实施,它就会起作用:

MethodWithoutParams someMethodImplementation = 
[TestClass methodForSelector:@selector(someMethod)];

所以我查看了NSObject实现并查看下一个代码:

+ (IMP)methodForSelector:(SEL)sel {
    if (!sel) [self doesNotRecognizeSelector:sel];
    return object_getMethodImplementation((id)self, sel);
}

- (IMP)methodForSelector:(SEL)sel {
    if (!sel) [self doesNotRecognizeSelector:sel];
    return object_getMethodImplementation(self, sel);
}

object_getMethodImplementation()函数has下一个实现:

IMP object_getMethodImplementation(id obj, SEL name)
{
    Class cls = (obj ? obj->getIsa() : nil);
    return class_getMethodImplementation(cls, name);
}

因此,类方法查找和实例方法查找的实现都是相同的。

但它不起作用,我不知道为什么。我想任何类型的方法(类和实例)都将位于调度表中,我可以获得指向任何此方法的指针。但正如你所见,我无法做到。

2 个答案:

答案 0 :(得分:6)

您需要查询班级的元类。类方法是其元类的实例方法。

Class meta = objc_getMetaClass("TestClass");
SEL sel = @selector(someMethod);
typedef void (*MyMethodImplementation)(Class, SEL);
MyMethodImplementation someMethodImplementation = (MyMethodImplementation)class_getMethodImplementation(meta, sel);
someMethodImplementation([TestClass class], sel);

注意,方法(实例或类)永远不会没有参数&#34;。必须始终至少使用接收器(实例或类)和选择器调用它。

答案 1 :(得分:1)

我完全不了解Objective-C运行时。但这是我的咆哮:

类方法的object_getMethodImplementation:

// returns a class method implementation, can't find instance methods
class_getMethodImplementation([TestClass class]->getIsa(),
                              @selector(classMethod));

相当于

class_getMethodImplementation(objc_getMetaClass("TestClass"),
                              @selector(classMethod));

object_getMethodImplementation实例方法:

// returns an instance method implementation, can't find class methods
class_getMethodImplementation((<TestClass instance>)->getIsa(),
                              @selector(someMethod));

相当于:

class_getMethodImplementation([TestClass class], 
                              @selector(someMethod))

所以<TestClass instance>->getIsa()似乎返回一个等于[TestClass class]返回的指针。我想你现在知道了类方法中的self和实例方法中的self之间的区别。

所以你应该使用元类作为@KenThomases建议。