NSInvocation

基本简介

NSInvocation是一个静态描绘的OC消息,也就是说,它是一个动作,这个动作可以变成一个对象。NSInvocation对象在对象和对象之间和应用程序和应用程序之间被用于存储和向前信息。

一个ISInvocation对象包括了所有OC消息的基本元素:目标,selector,参数和返回值。每个元素都可以直接设置,返回值是在NSInvocation对象发送的时候自动设置的。

一个NSInvocation对象可以被反复地发送给不同的目标;为了得到不同的结果,它的参数也可以在发送的时候直接修改;甚至它的selector也可以被修改为另一个,这个另一个和上一个需要有相同的方法签名(参数和返回类型)。这种灵活性使得NSInvocation非常有用在使用许多参数和变化的情况下重新发送消息,而不是为了发送消息而重新输入细小的改变。在发送消息到一个新的target前你可以修改NSInvocation对象。

NSInvocation不支持调用方法的参数。你应该使用invocationWithMethodSignature:这个类方法去创建NSInvocation对象,而不是使用alloc init.

例子

比如现在有个CurrentDate类,其中有个方法:

-(NSString *)stringForDate:(NSDate *)date usingFormatter:(NSDateFormatter *)formatter;

那么在ViewController中调用你可以有以下几种调用方式:

  • 原始调用

         NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setDateFormat:@"YYYY-MM-dd"];
    CurrentDate *currentDateClassObject = [[CurrentDate alloc] init];
    NSString *currentDate = [currentDateClassObject stringForDate:[NSDate date] usingFormatter:dateFormat]; NSLog(@"currentDate:%@",currentDate);
  • NSInvocation调用

     //NSInvocation调用
    //方法签名类,需要被调用消息所属的类CurrentDate,被调用的消息stringForDate:usingFormatter:
    SEL mySelector = @selector(stringForDate:usingFormatter:);
    NSMethodSignature *sig = [[currentDateClassObject class] instanceMethodSignatureForSelector:mySelector];
    //根据方法签名创建一个NSInvocation
    NSInvocation *myInvocation = [NSInvocation invocationWithMethodSignature:sig];
    //设置调用者
    [myInvocation setTarget:currentDateClassObject];
    //设置被调用的消息
    [myInvocation setSelector:mySelector];
    //如果此消息有参数需要传入,那么就需要按照如下方法进行参数设置,需要注意的是,atIndex的下标必须从2开始。原因为:0 1 两个参数已经被target 和selector占用
    NSDate *myDate = [NSDate date];
    [myInvocation setArgument:&myDate atIndex:2]; NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"YYYY-MM-dd"];
    [myInvocation setArgument:&dateFormatter atIndex:3];
    NSString *result = nil; //retain所有参数,防止参数被释放
    [myInvocation retainArguments];
    //消息调用
    [myInvocation invoke];
    //获取消息返回的信息
    [myInvocation getReturnValue:&result];
    NSLog(@"The result is :%@ ",result);

附:

NSInvocation的更多相关文章

  1. Objective-C中NSInvocation的使用

    OC中调用方法某个对象的消息呦两种方式: #1. performanceSelector: withObject: #2. NSInvocation. 第一个PerformaceSelector比较常 ...

  2. iOS开发——网络篇——UIWebview基本使用,NSInvocation(封装类),NSMethodSignature(签名),JavaScript,抛异常,消除警告

    一.UIWebView简介 1.UIWebView什么是UIWebViewUIWebView是iOS内置的浏览器控件系统自带的Safari浏览器就是通过UIWebView实现的 UIWebView不但 ...

  3. IOS NSInvocation用法简介

    IOS NSInvocation用法简介 2012-10-25 19:59 来源:博客园 作者:csj007523 字号:T|T [摘要]在 iOS中可以直接调用某个对象的消息方式有两种,其中一种就是 ...

  4. NSInvocation Basics

    In this article I'm going to cover the basics and usages of NSInvocation. What is NSInvocation? Appl ...

  5. NSInvocation的使用(转)

    转载自:http://www.cnblogs.com/pengyingh/articles/2359199.html http://blog.iosxcode4.com/?p=125 在 iOS中可以 ...

  6. ios NSMethodSignature and NSInvocation 消息转发

    1.首先获取消息转发时连个函数内部具体内容 MARK:这里是拿[@"xxxxx" length]调用拿来举例说明 (lldb) po signature <NSMethodS ...

  7. 利用NSInvocation对方法进行抽象,实现对方法的加锁

    我们在实际开发中须要对离散的方式加锁实现线程安全,当然我们有多种实现方式,这仅仅是当中一种,使用起来比較方便 + (id)performSelectorWithTarget:(id)target se ...

  8. iOS NSInvocation的学习

    用途: NSInvocation的作用和performSelector:withObject:的作用是一样的:用于iOS编程中调用某个对象的消息. performSelector:withObject ...

  9. 第16月第8天 NSInvocation存储 函数指针 va_arg lldb

    1.NSInvocation存储 -(void)setInvok:(id)target sel:(SEL)sel key:(id)key { if(!target) return; NSMethodS ...

随机推荐

  1. php 分页类

    <?php /* *本程序文件对分页程序进行了封装 * */ class Page_Link { var $page_max = 10; //一组页码的最大数 var $page_num = 1 ...

  2. repo: 创建local manifest以及如何添加app到CM/Android build系统中

    The local manifest Creating a local manifest allows you to customize the list of repositories on you ...

  3. 重写js alert

    Window.prototype.alert = function(){ //创建一个大盒子 var box = document.createElement("div"); // ...

  4. codeforces MUH and Cube Walls

    题意:给定两个序列a ,b, 如果在a中存在一段连续的序列使得 a[i]-b[0]==k, a[i+1]-b[1]==k.... a[i+n-1]-b[n-1]==k 就说b串在a串中出现过!最后输出 ...

  5. ES6入门系列一(基础)

    1.let命令 Tips: 块级作用域(只在当前块中有效) 不会变量提升(必须先申明在使用) 让变量独占该块,不再受外部影响 不允许重复声明 总之:let更像我们熟知的静态语言的的变量声明指令 ES6 ...

  6. SharePoint 2013中的默认爬网文件扩展名和分析文件类型

    摘要:了解默认情况下 SharePoint 2013 爬网的文件扩展名及其解析的文件类型,可以借此了解搜索可以爬的文件和支持的功能. 如果“管理文件类型”页上的列表包含文件扩展名,爬网组件将仅爬网文件 ...

  7. [python]在场景中理解装饰器

    原来我也自己通过查资料,来学习python的装饰器,但是效果不好.因为没有接触过需要用到装饰器的场景,所以 一起的资料都只停留在纸面上,但是今天偶然看到了vimer的这篇文章:http://www.v ...

  8. 结对编程之Fault、Error、Failure

    1.结对说明 结对对象:刘世麟  博客地址:http://www.cnblogs.com/liushilin/ 双方贡献:1:1 2.题目要求  构造程序,分别是:         •不能触发Faul ...

  9. EF封装类 增加版,增加从缓存中查找数据方法,供参考!

    EF封装类 增加版,增加从缓存中查找数据方法,供参考! 这个类是抽象类,我这里增加了需要子类验证的方法ValidateEntity,方便扩展,若想直接使用该类,可以将该类更改成静态类,里面所有的方法都 ...

  10. EF封装类,供参考!

    以下是我对EF DB FIRST 生成的ObjectContext类进行封装,代码如下,供参考学习: using System; using System.Collections.Generic; u ...