简体   繁体   English

如何通过NSInvocation调用类方法?

[英]How can I invoke a class method by NSInvocation?

We can get NSMethodSignature by + (NSMethodSignature *)instanceMethodSignatureForSelector:(SEL)aSelector of NSObject . 我们可以通过+ (NSMethodSignature *)instanceMethodSignatureForSelector:(SEL)aSelector NSObject + (NSMethodSignature *)instanceMethodSignatureForSelector:(SEL)aSelector来获取NSMethodSignature Then construct NSInvocation by + (NSInvocation *)invocationWithMethodSignature:(NSMethodSignature *)signature 然后通过+ (NSInvocation *)invocationWithMethodSignature:(NSMethodSignature *)signature构造NSInvocation

Here is the problem. 这是问题所在。 We can only get the method signature of instance ,how about the class method? 我们只能得到实例的方法签名,类方法怎么样?

You can get the class methods by: 您可以通过以下方式获取类方法:

NSMethodSignature *pMS = [[YourObject class] methodSignatureForSelector: (SEL)aSelector];

The fragment [YourObject class] returns an instance of the class object (singleton) which you can then use to get class methods. 片段[YourObject class]返回类对象(singleton)的一个实例,然后您可以使用它来获取类方法。

ADD: New info from comments below. 添加:以下评论的新信息。 You can just do the following: 您可以执行以下操作:

NSMethodSignature *pMS = [YourObject methodSignatureForSelector: (SEL)aSelector];

Here is additional detail about invoking class (and instance methods) via NSInvocation showing methodForSignature: and instanceMethodForSignature: 以下是有关通过显示methodForSignature:instanceMethodForSignature: NSInvocation调用类(和实例方法)的其他详细信息instanceMethodForSignature:

@interface SomeObject : NSObject
+ (void)instanceAndClass;
- (void)instanceAndClass;
+ (void)classOnly;
- (void)instanceOnly;
@end

@implementation SomeObject
+ (void)instanceAndClass { NSLog(@"Class version"); }
- (void)instanceAndClass { NSLog(@"Instance version"); }    
+ (void)classOnly { NSLog(@"Class only"); }
- (void)instanceOnly { NSLog(@"Instance only"); }
@end

@implementation Foo

- (void)invoke:(id)receiver signature:(NSMethodSignature *)signature selector:(SEL)selector {
    if (!signature) {
        NSLog(@"method signature is nil");
        return;
    }        
    NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:signature];
    invocation.selector = selector;
    invocation.target = receiver;
    [invocation invoke];
}

- (void)foo {
    NSMethodSignature *signature;
    SomeObject *someObject = [SomeObject new];

    signature = [someObject methodSignatureForSelector:@selector(instanceAndClass)];
    [self invoke:someObject signature:signature selector:@selector(instanceAndClass)];
    // 2015-07-10 11:01:10.227 FooBar[65312:3097866] Instance version
    [self invoke:[someObject class] signature:signature selector:@selector(instanceAndClass)];
    // 2015-07-10 11:01:10.227 FooBar[65312:3097866] Class version

    signature = [someObject methodSignatureForSelector:@selector(classOnly)];
    [self invoke:someObject signature:signature selector:@selector(classOnly)];
    // 2015-07-10 11:01:10.227 FooBar[65312:3097866] method signature is nil

    signature = [[someObject class] methodSignatureForSelector:@selector(classOnly)];
    [self invoke:[someObject class] signature:signature selector:@selector(classOnly)];
    // 2015-07-10 11:01:10.228 FooBar[65312:3097866] Class only

    signature = [someObject methodSignatureForSelector:@selector(instanceOnly)];
    [self invoke:someObject signature:signature selector:@selector(instanceOnly)];
    // 2015-07-10 11:01:10.228 FooBar[65312:3097866] Instance only

    signature = [[someObject class] methodSignatureForSelector:@selector(instanceOnly)];
    [self invoke:[someObject class] signature:signature selector:@selector(instanceOnly)];
    2015-07-10 11:01:10.228 FooBar[65312:3097866] method signature is nil

    signature = [[someObject class] instanceMethodSignatureForSelector:@selector(instanceOnly)];
    [self invoke:someObject signature:signature selector:@selector(instanceOnly)];
    // 2015-07-10 11:01:10.228 FooBar[65312:3097866] Instance only
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM