• 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

  • 相关阅读:
    第七章 深入理解多态
    第六章 继承和多态
    第五章 体验套餐管理系统
    第四章 深入类的方法
    第三章 使用集合组织相关数据
    第二章 深入C#数据类型
    mysql 创建表字段类型笔记
    梳理下phpmyadmin改root密码后登录不上的问题。
    easyui 验证控件 tooltip message显示位置
    random模块详解
  • 原文地址:https://www.cnblogs.com/wobuyayi/p/6841084.html
Copyright © 2020-2023  润新知