zoukankan      html  css  js  c++  java
  • iOS中使用NSInvocation

    在iOS中可以使用NSInvocation进行动态调用方法。

    /*
    NSInvocation is much slower than objc_msgSend()...
    Do not use it if you have performance issues.
    */

    ViewController.h

    #import <UIKit/UIKit.h>
    @interface ViewController : UIViewController
    @end

    ViewController.m

    #import "ViewController.h"
    
    @interface ViewController ()
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
        
        //1.创建 NSMethodSignature
        NSMethodSignature *sig = [self methodSignatureForSelector:@selector(sendMessageWithStr1:andStr2:andStr3:)];
        if (!sig) {
            [self doesNotRecognizeSelector:@selector(sendMessageWithStr1:andStr2:andStr3:)];
            return;
        }
        
        //2.根据 NSMethodSignature 创建 NSInvocation
        NSInvocation *inv = [NSInvocation invocationWithMethodSignature:sig];
        if (!inv) {
            [self doesNotRecognizeSelector:@selector(sendMessageWithStr1:andStr2:andStr3:)];
            return;
        }
        
        //3.设置代理和Selector
        [inv setTarget:self];
        [inv setSelector:@selector(sendMessageWithStr1:andStr2:andStr3:)];
        
        //4.设置参数
        NSString *str1 = @"111";
        NSString *str2 = @"222";
        NSString *str3 = @"333";
        
        //注意:设置参数的索引时不能从0开始,因为0已经被self占用,1已经被_cmd占用,可变参数可以使用va_list和va_start,具体参考参考YYKit中的NSObject+YYAdd.m
        [inv setArgument:&str1 atIndex:2];
        [inv setArgument:&str2 atIndex:3];
        [inv setArgument:&str3 atIndex:4];
        
        //5.调用方法
        [inv invoke];
    }
    
    - (void)sendMessageWithStr1:(NSString *)str1 andStr2:(NSString *)str2 andStr3:(NSString *)str3{
        NSLog(@"str1:%@,str2:%@,str3:%@",str1,str2,str3);
    }
    
    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }
    
    @end

    运行截图:

    参考文章:

    http://www.jianshu.com/p/da96980648b6

  • 相关阅读:
    内容居中
    ajax验证登录注册
    html5 图片转为base64格式异步上传
    关于iframe的滚动条,如何去掉水平滚动条或垂直滚动条
    oracle数据库操作(未封装)
    oracle数据库操作(结合读取.ini文件操作)
    读取xml文件的方法
    线程练习
    TypeError: the JSON object must be str, not 'bytes'报错问题解决
    python datetime.datetime is not JSON serializable 报错问题解决
  • 原文地址:https://www.cnblogs.com/wobuyayi/p/6841084.html
Copyright © 2011-2022 走看看