美文网首页
NSInvocation

NSInvocation

作者: Rumbles | 来源:发表于2019-01-18 17:49 被阅读9次

在iOS开发中,我们一般会使用以下两种方式去调用一个方法

(1) [obj methodName];
(2) [obj performSelector......];

为什么使用方法2 ?看下面需求 方式一不能通过方法名字符串来执行方法

需求1.:通过字符串调用方法

    NSString *str = @"invocationThisMethord:";
    [self performSelector:NSSelectorFromString(str) withObject:@"name" afterDelay:0];

- (void)invocationThisMethord:(NSString *)name {
    NSLog(@"invocationThisMethord %@",name);
}
 完美解决需求

需求2.:通过字符串调用下面方法

//方法名字符串
NSString *methodNameStr = @"test:withArg2:andArg3:";

我们知道[obj performSelector......]; 参数有限  怎么办?

所以我们就需要用到了NSInvocation
       //1.创建一个方法签名
    NSMethodSignature *signature = [self methodSignatureForSelector:NSSelectorFromString(methodNameStr)];
    //2.使用方法的签名来创建一个NSInvocation对象
    NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:signature];
    //3.给invocation的两个属性赋值 要执行谁的(target)的哪个方法(selector)
    invocation.target = self;
    invocation.selector = NSSelectorFromString(methodNameStr);
    
    //4.给要执行的方法设置参数 使用setArgument:atIndex:方法给要执行的方法设置参数,注意下标从2开始,因为0、1已经被target与selector占用
    NSString *param1 = @"param1";
    NSString *param2 = @"param2";
    NSString *param3 = @"param3";
    [invocation setArgument:&param1 atIndex:2];
    [invocation setArgument:&param2 atIndex:3];
    [invocation setArgument:&param3 atIndex:4];
    //5.执行方法
    [invocation invoke];

需求3.:通过字符串调用下面方法

NSString *methodNameStr = @"test:withArg2:andArg3:";
//要执行的方法
- (NSString *)test:(NSString *)arg1 withArg2:(NSString *)arg2 andArg3:(NSString *)arg3 {
    NSLog(@"%@---%@---%@", arg1, arg2, arg3);
}

    NSString *methodNameStr = @"test:withArg2:andArg3:";
    
    NSMethodSignature *signature = [self methodSignatureForSelector:NSSelectorFromString(methodNameStr)];
    NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:signature];
    invocation.target = self;
    invocation.selector = NSSelectorFromString(methodNameStr);
    
    NSString *param1 = @"param1";
    NSString *param2 = @"param2";
    NSString *param3 = @"param3";
    [invocation setArgument:&param1 atIndex:2];
    [invocation setArgument:&param2 atIndex:3];
    [invocation setArgument:&param3 atIndex:4];
    [invocation invoke];
    
    if (signature.methodReturnLength > 0) {
        NSString *result;
        [invocation getReturnValue:&result];
        NSLog(@"%@",result);
    }

2019-01-18 17:48:41.350282+0800 Test[13957:7661495] param1---param2---param3
2019-01-18 17:48:41.350437+0800 Test[13957:7661495] lalallallala

相关文章

  • BlocksKit A2DynamicDelegate研究

    NSInvocation @interface NSInvocation:NSObject 通过方法签名获得NSI...

  • NSInvocation如何调用block

    NSInvocation如何调用block 同步发布到博客地址NSInvocation如何调用block NSIn...

  • NSInvocation个人理解

    NSInvocation的使用: //NSInvocation;用来包装方法和对应的对象,它可以存储方法的名称,对...

  • 强大的NSInvocation

    前言 在消息转发中提到过NSInvocation这个类,这里说一下我所理解的NSInvocation。NSInvo...

  • NSInvocation使用

    NSInvocation NSInvocation是一个消息调用类,主要作用是存储和传递消息。它存储的信息包含了一...

  • void SendDelegateMessage(NSInvoc

    void SendDelegateMessage(NSInvocation *): delegate (webVi...

  • 使用 Invocation 来动态调用

    NSInvocation 可以方便的调用

  • NSInvocation

    在 iOS中可以直接调用某个对象的消息方式有三种:一种是[self xxxxxxx];直接对象调用另一种是 pe...

  • NSInvocation

    NSInvocation是命令模式的一种实现,它包含选择器、方法签名、相应的参数以及目标对象。所谓的方法签名,即方...

  • NSInvocation

    NSInvocation 在 iOS中可以直接调用某个对象的消息方式有两种 一种是performSelector:...

网友评论

      本文标题:NSInvocation

      本文链接:https://www.haomeiwen.com/subject/ofandqtx.html