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. WCF 遇到的问题

    1.只有项目的net版本2.0以上的才可以引用到wcf的类库 2.HTTP 错误 404.17 - Not Found  映射问题   WCF服务建立好,提示这个错误,缺少映射问题,要将应用程序池和项 ...

  2. HTML5[7]: 实现网页版的加载更多

    理所当然,jQuery出场: $(function() { $(window).scroll(function() { if($(this).scrollTop() + $(this).innerHe ...

  3. Nginx 单机百万QPS环境搭建

    一.背景 最近公司在做一些物联网产品,物物通信用的是MQTT协议,内部权限与内部关系等业务逻辑准备用HTTP实现.leader要求在本地测试中要模拟出百万用户同时在线的需求.虽然该产品最后不一定有这么 ...

  4. 基于jQuery点击加载动画按钮特效

    分享一款基于jQuery点击加载动画按钮特效.这是一款基于jQuery+CSS3实现的鼠标点击按钮加载动画特效代码.效果图如下: 在线预览   源码下载 实现的代码. html代码: <div ...

  5. 【HTML】Iframe中的onload事件

    当iframe.src重新指定一个url时会重新执行iframe的onload事件 <iframe id="indexFrame" name="index" ...

  6. Cwinux简介及用法简述

    我在我的个人博客上发表了一篇文章 Cwinux简介及用法简述 http://apprentice89.com/cwinux_introduction_and_use/

  7. bootstrap插件学习-bootstrap.button.js

    先看bootstrap.button.js的结构 var Button = function ( element, options ){} //构造器 Button.prototype = {} // ...

  8. python关键字详解

    今天依旧在啃:<笨方法学python>,其中习题37是复习各种关键字.我本想百度一下记一下就ok了,但是百度出来第一个就Hongten的博客.我才意识到我也有博客,我应该学习他,把这些积累 ...

  9. stl的优先级队列

    #include <iostream> #include <vector> #include <queue> using namespace std; class ...

  10. 在Linux上安装Oracle RAC 12 c(12.1) 虚拟机,一步一步向导

    Oracle RAC 12 c(12.1)在Linux上安装虚拟机,一步一步向导 今天我们将看到如何安装 12 c版本1 RAC(真正的应用程序集群)数据库2 Linux 64位的虚拟机 使用VMWa ...