NSInvocation
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的更多相关文章
- Objective-C中NSInvocation的使用
OC中调用方法某个对象的消息呦两种方式: #1. performanceSelector: withObject: #2. NSInvocation. 第一个PerformaceSelector比较常 ...
- iOS开发——网络篇——UIWebview基本使用,NSInvocation(封装类),NSMethodSignature(签名),JavaScript,抛异常,消除警告
一.UIWebView简介 1.UIWebView什么是UIWebViewUIWebView是iOS内置的浏览器控件系统自带的Safari浏览器就是通过UIWebView实现的 UIWebView不但 ...
- IOS NSInvocation用法简介
IOS NSInvocation用法简介 2012-10-25 19:59 来源:博客园 作者:csj007523 字号:T|T [摘要]在 iOS中可以直接调用某个对象的消息方式有两种,其中一种就是 ...
- NSInvocation Basics
In this article I'm going to cover the basics and usages of NSInvocation. What is NSInvocation? Appl ...
- NSInvocation的使用(转)
转载自:http://www.cnblogs.com/pengyingh/articles/2359199.html http://blog.iosxcode4.com/?p=125 在 iOS中可以 ...
- ios NSMethodSignature and NSInvocation 消息转发
1.首先获取消息转发时连个函数内部具体内容 MARK:这里是拿[@"xxxxx" length]调用拿来举例说明 (lldb) po signature <NSMethodS ...
- 利用NSInvocation对方法进行抽象,实现对方法的加锁
我们在实际开发中须要对离散的方式加锁实现线程安全,当然我们有多种实现方式,这仅仅是当中一种,使用起来比較方便 + (id)performSelectorWithTarget:(id)target se ...
- iOS NSInvocation的学习
用途: NSInvocation的作用和performSelector:withObject:的作用是一样的:用于iOS编程中调用某个对象的消息. performSelector:withObject ...
- 第16月第8天 NSInvocation存储 函数指针 va_arg lldb
1.NSInvocation存储 -(void)setInvok:(id)target sel:(SEL)sel key:(id)key { if(!target) return; NSMethodS ...
随机推荐
- Fidder模拟Post请求
背景 最近想用fidder模拟post请求,怎么都传值失败,发现写Composer => Request Body中写的内容,总是无法映射到mvc的action参数上.百度一番,发现如下解决方案 ...
- User Settings in WPF
原文:<User Settings in WPF> Posted on 2014/04/09 =============================================== ...
- jsp,OGNL调用后台Action的某方法
用%{}可取出valueStack中的Action,可直接调用其方法. %{testa('key')} 即可调用到action的testa(String s) 方法 但这些都需要结合struts2的标 ...
- ruby -- 问题解决(四)编码错误导致无法显示(2)
从数据库中取得数据显示时报 incompatible character encodings: GBK and ASCII-8BIT或 incompatible character encodings ...
- 关于MySQL的Admin Ping Command
前言: 最近在线上诊断QPS飙升的过程中深入进行了下Admin Ping Command的测试.此外,再一些国外文章中最近也读到了一些相关知识,所以写成一篇博文做一下总结. 1. 关于Admin Pi ...
- LeetCode—— Median of Two Sorted Arrays
Description: There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the medi ...
- 后缀数组 --- HDU 3518 Boring counting
Boring counting Problem's Link: http://acm.hdu.edu.cn/showproblem.php?pid=3518 Mean: 给你一个字符串,求:至少出 ...
- UnityShader快速上手指南(二)
简介 前一篇介绍了如果编写最基本的shader,接下来本文将会简单的深入一下,我们先来看下效果吧 呃,gif效果不好,实际效果是很平滑的动态过渡 实现思路 1.首先我们要实现一个彩色方块 2.让色彩动 ...
- [CLR via C#]21. 自动内存管理(垃圾回收机制)
目录 理解垃圾回收平台的基本工作原理 垃圾回收算法 垃圾回收与调试 使用终结操作来释放本地资源 对托管资源使用终结操作 是什么导致Finalize方法被调用 终结操作揭秘 Dispose模式:强制对象 ...
- SVN 忽略文件但不删除文件
SVN忽略一些不必要的文件但不删除 如果svn仓库中有一些不希望被别人提交的文件 该如何忽略掉对这个文件的更改但又不删除这个文件呢? 在找了一堆解决方案后得出了如下结论 去除要被忽略文件的版本控制 基 ...