每日更新关注:http://weibo.com/hanjunqiang 
新浪微博

1.SEL触发

SEL就是selector的缩写,它表示Cocoa中的方法选择器,不明白?那请仔细了解Objective_C的运行时机制与Cocoa底层思想。

SEL theSelector = @selector(methodWithInt:andInt:);

看了上面代码这下你明白了什么是SEL了吧,平时我们开发当中经常用到的。

有了SEL这样就可以触发方法调用了,

[self performSelector:theSelector]
[self performSelector:@selector(methodWithInt:andInt:)];

上面两句代码是同一个道理。

2. IMP触发

IMP其实就是一个函数指针的概念,就可以这么简单理解。

IMP theImplementation = [self methodForSelector:theSelector];

上面这句代码就是获取methodWithInt:andInt:这个方法的地址。

有了这个函数指针后,我们就可以触发方法:

theImplementation(self, theSelector, 30, 5);

第一个是对象,第二个SEL, 后面的传入的参数。

每日更新关注:http://weibo.com/hanjunqiang 
新浪微博

3. objc_msgSend方法

这是Objc运行时的一个C方法,我们先看一个示列:

objc_msgSend(self, @selector(fly));

[self fly];

4. NSInvocation

NSInvocation相比上面几种方法,可以动态决定传入的参数个数。有了它,我们就可以实现…这样的变参API封装。

说得有点抽像,看代码更清楚:

假如我们实现了一个两个数相加的方法,

-(void)addNSNumber:(NSNumber *)first withNumber:(NSNumber *)second

我们可以用下面的方法来触发这个方法,并获取返回值。

NSNumber *arg1 = [NSNumber numberWithDouble:15.0];
NSNumber *arg2 = [NSNumber numberWithDouble:13.0];
 SEL selector = @selector(addNSNumber:withNumber:);
NSMethodSignature *sig = [self methodSignatureForSelector:selector];
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:sig];
[invocation setTarget:self]; [invocation setSelector:selector];
[invocation setArgument:(void *)&arg1 atIndex:2];
 [invocation setArgument:(void *)&arg2 atIndex:3];
 [invocation invoke]; if ([sig methodReturnLength])
 { [invocation getReturnValue:&retval]; return retval; } return nil; }

代码很容易理解,我也不多做解释,有不明白的请留言。

每日更新关注:http://weibo.com/hanjunqiang 
新浪微博

下面是变参封装, 写了两个NSObject的Extension(Category):

 
+ (NSInvocation *)createInvocationOnTarget:(id)target selector:(SEL)selector {
  return [NSObject createInvocationOnTarget:target selector:selector withArguments:nil];
}

+ (NSInvocation *)createInvocationOnTarget:(id)target selector:(SEL)selector withArguments:(id)arg1, ... {
  NSMethodSignature *sig = [target methodSignatureForSelector:selector];
  NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:sig];

  [invocation setTarget:target];
  [invocation setSelector:selector];

  if(arg1) {
      va_list args;
      va_start(args, arg1);

      [invocation setArgument:(void *)&arg1 atIndex:2];

      id obj;
      int ct = 3;

      while( obj = va_arg(args, id) ) {
          NSLog(@"%@", obj);
          [invocation setArgument:(void *)&obj atIndex:ct];

          ct++;
      }

      va_end(args);
  }

  return invocation;
}

调用方法:

 
NSInvocation *invocation = [NSObject createInvocationOnTarget:mathInstance selector:selector withArguments: arg1, arg2, nil]; 

[invocation invoke];
[invocation getReturnValue:&retval];

return retval;

每日更新关注:http://weibo.com/hanjunqiang 
新浪微博

Cocoa触发方法调用的几种方法的更多相关文章

  1. struts调用的几种方法

    在Struts2中方法调用概括起来主要有三种形式 第一种方式:指定method属性 <action name="student" class="com.itmyho ...

  2. Struts2方法调用的三种方式

    在Struts2中方法调用概括起来主要有三种形式 第一种方式:指定method属性 <action name="student" class="com.itmyho ...

  3. Struts2方法调用的三种方式(有新的!调用方法的说明)

    在Struts2中方法调用概括起来主要有三种形式 第一种方式:指定method属性 <action name="heroAction" class="com.ABC ...

  4. 第164天:js方法调用的四种模式

    js方法调用的四种模式 1.方法调用模式 function Persion() { var name1 = "itcast", age1 = 19, show1 = functio ...

  5. QT下实现对Linux Shell调用的几种方法

    使用QProcess QThread ============================================ #include <QProcess>int main(){ ...

  6. 【java】子类可以通过调用父类的public方法调用父类的private方法,为什么?

    代码1: 打印结果: 代码2: 运行结果: 问题: 代码1中super是父类自己调用自己的add()方法,并在add()方法中调用了私有的del()方法,那为什么打印出来的this是子类? 代码2中t ...

  7. vue中触发键盘事件的两种方法和如何自定义键位事件,完整代码!

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  8. Android延时执行调用的几种方法

    一.开启新线程 new Thread(new Runnable(){        public void run(){            Thread.sleep(XXXX);          ...

  9. php 递归调用又一种方法

     public static function encodeXml($data){        $attr = $xml = "";        foreach($data a ...

随机推荐

  1. 新版Eclipse打开jsp、js等为文本编辑,没有JSP Editor插件问题

    刚从官网下载安装的Eclipse Java Oxygen.2但是打开的jsp文件尽然默认文本编辑器打开,就js文件也是一样,纳闷! 网上搜索一番,原来缺少web开发相关工具, 下面给插件安装方法: 1 ...

  2. 异步 JavaScript 之 macrotask、microtask

    1.异步任务运行机制 先运行下面的一段代码: console.log('script start'); setTimeout(function() { console.log('setTimeout' ...

  3. SQL之LIMIT ,OFFSET

    SELECT prod_name FROM Products LIMIT OFFSET ; LIMIT 4 OFFSET 3指示MySQL等DBMS返回从第3行(从0行计数)起的4行数据.第一个数字是 ...

  4. oss web直传

    签名信息 auth.php <?php function gmt_iso8601($time) { $dtStr = date("c", $time); $mydatetim ...

  5. Lintcode394 Coins in a Line solution 题解

    [题目描述] There are n coins in a line. Two players take turns to take one or two coins from right side ...

  6. android基础-界面开发注意事项

    做安卓开发时一定要注意,主线程不能更改UI界面,如果出现程序运行时崩溃的情况,如果没有明显的语法错误,请检查自己的进程是否出现冲突,崩溃.如果有与后台的连接,即请求向服务器发送请求的时尤其需要注意,或 ...

  7. SAS中常见的数组函数

    SAS中常见的数组函数有: dim dimk hbound hboundk lbound lboundk 数组函数计萁数组的维数.上下界,有利于写出可移植的程序,数组函数包括:dim(x) 求数组x第 ...

  8. 阿里云linux下web服务器配置

    markdown截图不方便,本教程不用markdown编写 首先参考文章 https://www.jianshu.com/p/2604e53a7f6a?from=singlemessage 安装完后无 ...

  9. U盘PE无人值守安装centOS6

    一.制作 1.需要用到的工具:老毛桃PX工具.系统ISO.一个8GU盘 老毛桃PE工具 http://laomaotao.net/ CentOS启动映像 http://mirrors.163.com/ ...

  10. Python3 错误和异常

    Python有两种错误很容易辨认:语法错误和异常. 语法错误 Python 的语法错误或者称之为解析错,是初学者经常碰到的,如下实例 >>> while True print('He ...