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

What is NSInvocation?

Apple Developer Reference describes NSInvocation this way:

An NSInvocation is an Objective-C message rendered static, that is, it is an action turned into an object. NSInvocation objects are used to store and forward messages between objects and between applications, primarily by NSTimer objects and the distributed objects system.

When we call a method or access a member data of an object in our application, we are actually sending a message. Usually we don't send messages explicitly in our code since the compiler does the work for us. What NSInvocation brings to us is the ability to manually send a message to an object. So a NSInvocation object's job is to send messages; hence we create an object of NSInvocation class, we describe the aspects of the message and finally we send it to our target. The most useful advantage of NSInvocation is that it's an object so we can pass it as an argument to other methods.

Simple NSInvocation example

Suppose we have a dummy object named myDummyObject and it is an object of class myClass. in myClass we have a defined method named sayHelloWorld without any arguments. The simplest way to execute sayHelloWorld method is:

[myDummyObject sayHelloWorld];
1:

Select allOpen in new window

In fact this line causes the compiler to create a message which says "execute sayHelloWorld method" and sends it to the target object which is myDummyObject here.
Now we want to create the same message manually and send it to myDummyObject using NSInvocation.

A selector describes which method we want to call:

SEL selector = @selector(sayHelloWorld);
1:

Select allOpen in new window

Our NSInvocation object needs to know whether the target object has declared the method or if not throws an exception, so it should utilize some search algorithm to achieve this: A method signature is fingerprint of a method which depends on its return value, number of arguments it takes and their types. Therefore our NSInvocation will be able to search through the target object's methods list to see whether it has defined the method or not. This signature is created using methodSignatureForSelector method which every NSObject descendent has this method. In this case sayHelloWorld is a method of myClass. myDummyObject is an object of myClass so we should call methodSignatureForSelector on myDummyObject to return the signature for the method named sayHelloWorld using the selector we declared above:

NSMethodSignature *signature = [myDummyObject methodSignatureForSelector:selector];
1:

Select allOpen in new window

We create our NSInvocation object based on the method signature we declared:

        NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:signature];
1:

Select allOpen in new window

NSInvocation objects also wants to have our selector:

        [invocation setSelector:selector];
1:

Select allOpen in new window

Here we specified our target object which we want our message to be sent to:

        [invocation setTarget:myDummyObject];
1:

Select allOpen in new window

The final step here. Commanding our NSInvocation object to send the message.

        [invocation invoke];
1:

Select allOpen in new window

NSInvocation to call a method with arguments

Suppose myDummyObject contains a method as follows:

-(void)myFunctionWithArg1: (NSString*) arg1 Arg2: (NSString *) arg2 Arg3: (NSString *) arg3 Arg4: (NSString *) arg4
{
NSLog(@"I am a function with 4 arguments: <%@> <%@> <%@> <%@>" , arg1, arg2, arg3, arg4);
}
1:
2:
3:
4:

Select allOpen in new window

The normal way of calling this method is:

[myDummyObject myFunctionWithArg1:@"First" Arg2:@"Second" Arg3:@"Third" Arg4:@"Fourth"];
1:

Select allOpen in new window

Now we want to create the message manually using NSInvocation. As I explained above:

SEL selector = @selector(myFunctionWithArg1:Arg2:Arg3:Arg4:);
NSMethodSignature *signature = [myDummyObject methodSignatureForSelector:selector];
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:signature];
[invocation setTarget:myDummyObject];
[invocation setSelector:selector];
1:
2:
3:
4:
5:

Select allOpen in new window

We define four strings. We will pass them through our NSInvocation toward the target object:

    NSString *arg1 = @"First";
NSString *arg2 = @"Second";
NSString *arg3 = @"Third";
NSString *arg4 = @"Fourth";
1:
2:
3:
4:

Select allOpen in new window

This is the way to set the arguments for the method. You can use SetArgument:atIndex for each argument at the specified index. 
Some notes about SetArgument:atIndex method:
1- The first argument should be the address of the variable you want to set. (You may encounter bridging if you are on ARC mode)
2- Indexes start at 2 so e.g. index 2 is the first argument of the method.

    [invocation setArgument:&arg1 atIndex:2];
[invocation setArgument:&arg2 atIndex:3];
[invocation setArgument:&arg3 atIndex:4];
[invocation setArgument:&arg4 atIndex:5];
1:
2:
3:
4:

Select allOpen in new window

And finally invoking our message:

    [invocation invoke];
1:

Select allOpen in new window

Using NSInvocation for call-backs

The most interesting usage of NSInvocation is that we can setup a call-back method for another method using NSInvocation. Now you may ask "what is a call-back method?" There are many asynchronous operations such as NSURLConnection (which is bound to transfer some data through a network) or NSXMLParser (which has the duty to parse a XML document). Most times these operations do not return their results immediately. We can request and setup many of such operations to call our desired methods (call-back method) when they are done (may also pass the result to our call-back method through arguments).

NSInvocation with call-back example

Suppose myDummyObject has a method named processMyTasksWithCallBackFunction as follows: processMyTasksWithCallBackFunction dispatches its tasks asynchronously using GCD.

-(void)processMyTasksWithCallBackFunction: (NSInvocation *) invocation
{
dispatch_async(dispatch_get_main_queue(), ^{ @autoreleasepool {
NSLog(@"Performing tasks like accessing a web service or preparing data for app");
sleep(3);//waiting 3 seconds
NSLog(@"DONE! Now calling the callback function..."); ///////// tasks are done; return the results through the NSInvocation object
///////// NSInvocation is supposed to point to a method which takes 4 NSString arguments NSString *arg1 = @"Result1";
NSString *arg2 = @"Result2";
NSString *arg3 = @"Result3";
NSString *arg4 = @"Result4";
[invocation setArgument:&arg1 atIndex:2];
[invocation setArgument:&arg2 atIndex:3];
[invocation setArgument:&arg3 atIndex:4];
[invocation setArgument:&arg4 atIndex:5];
[invocation invoke];
}
});
}
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:

Select allOpen in new window

Now we want to call processMyTasksWithCallBackFunction method and grab the results. Before that we have to declare a call-back method which takes 4 arguments (in our example). We declare this method in current class where we are calling processMyTasksWithCallBackFunction from. (Of course we can setup the call-back method from a remote object too.)

-(void)myFunctionWithArg1: (NSString*) arg1 Arg2: (NSString *) arg2 Arg3: (NSString *) arg3 Arg4: (NSString *) arg4
{
NSLog(@"I am a call-back method with 4 arguments: <%@> <%@> <%@> <%@>" , arg1, arg2, arg3, arg4);
}
1:
2:
3:
4:

Select allOpen in new window

Now we have to create the NSInvocation for our call-back method to be passed to processMyTasksWithCallBackFunction. Notice we don't set the arguments here because processMyTasksWithCallBackFunction will set them when it's done.

    SEL CB_selector = @selector(myFunctionWithArg1:Arg2:Arg3:Arg4:);
NSMethodSignature *CB_signature = [self methodSignatureForSelector:CB_selector];
NSInvocation *CB_invocation = [NSInvocation invocationWithMethodSignature:CB_signature];
[CB_invocation setTarget:self];//Since we declared the call-back method in current class
[CB_invocation setSelector:CB_selector];
1:
2:
3:
4:
5:

Now we can call the asynchronous method:

[processMyTasksWithCallBackFunction: CB_invocation];
1:
Sample Output:
2011-09-13 12:35:35.601 InvocationTest[6710:4903] Performing tasks like accessing a web service or preparing data for app
2011-09-13 12:35:38.603 InvocationTest[6710:4903] DONE! Now calling the callback function...
2011-09-13 12:35:38.604 InvocationTest[6710:4903] I am a call-back method with 4 arguments: <Result1> <Result2> <Result3> <Result4>

Conclusion

NSInvocation can bring great flexibility to your applications/frameworks in many situations. I hope this clarified NSInvocation and its usages. Please let me know if you have any question.

NSInvocation Basics的更多相关文章

  1. Objective-C中NSInvocation的使用

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

  2. Assembler : The Basics In Reversing

    Assembler : The Basics In Reversing Indeed: the basics!! This is all far from complete but covers ab ...

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

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

  4. The Basics of 3D Printing in 2015 - from someone with 16 WHOLE HOURS' experience

    全文转载自 Scott Hanselman的博文. I bought a 3D printer on Friday, specifically a Printrbot Simple Metal fro ...

  5. Cadence UVM基础视频介绍(UVM SV Basics)

    Cadence关于UVM的简单介绍,包括UVM的各个方面.有中文和英文两种版本. UVM SV Basics 1 – Introduction UVM SV Basics 2 – DUT Exampl ...

  6. C basics

    C 日记目录 C basics ................ writing Numeration storage   , structor space assigning pointer,  a ...

  7. Xperf Basics: Recording a Trace(转)

    http://randomascii.wordpress.com/2011/08/18/xperf-basics-recording-a-trace/   This post is obsolete ...

  8. Xperf Analysis Basics(转)

      FQ不易,转载 http://randomascii.wordpress.com/2011/08/23/xperf-analysis-basics/ I started writing a des ...

  9. Radio Basics for RFID

    Radio Basics for RFID The following is excerpted from Chapter 3: Radio Basics for UHF RFID from the ...

随机推荐

  1. Jquery源码中的Javascript基础知识(四)— jQuery.fn.init方法

    $() 即调用了jQuery.fn.init方法 jQuery = function( selector, context ) { return new jQuery.fn.init( selecto ...

  2. 基于CentOS与VmwareStation10搭建Oracle11G RAC 64集群环境:2.搭建环境-2.2安装操作系统CentOS5.4

    2.2. 安装操作系统CentOS5.4 两个虚拟机都安装,此步骤在创建虚拟机节点时: 基于CentOS与VmwareStation10搭建Oracle11G RAC 64集群环境所有链接: 1.资源 ...

  3. 在Linux上安装多Jboss个需要修改的端口

    如果在一台机器上部署了多个jboss server,需要修改相关端口以避免端口冲突.目前确认需要修改的配置如下一.vi $JBOSS_HOME/server/default/conf/jboss-se ...

  4. Linux makefile教程之隐含规则九[转]

    隐含规则 ———— 在 我们使用Makefile时,有一些我们会经常使用,而且使用频率非常高的东西,比如,我们编译C/C++的源程序为中间目标文件(Unix下是[.o] 文件,Windows下是[.o ...

  5. MVC和WebApi 使用get和post 传递参数。

    我们总结一下用js请求服务器的传参方法. Get方式 Get主要是用来查询,一般分为无参,一个参数,多个参数,实体对象参数. 1.无参 //Get没有参数 var get_f1 = function( ...

  6. c#中const、static、readonly的区别

    1. const与readonly const ,其修饰的字段只能在自身声明时初始化. Readonly 是只读变量,属于运行时变量,可以在类初始化的时候改变它的值.该类型的字段,可以在声明或构造函数 ...

  7. LoadRunner 各个指标分析

    我们要监视CPU,内存.硬盘的资源情况.得到以下的参数提供分析的依据.%processor time(processor_total):器消耗的处理器时间数量.如果服务器专用于sql server 可 ...

  8. 《VC++ 6简明教程》即VC++ 6.0入门精讲 学习进度及笔记

    VC++6.0入门→精讲 2013.06.09,目前,每一章的“自测题”和“小结”三个板块还没有看(备注:第一章的“实验”已经看完). 2013.06.16 第三章的“实验”.“自测题”.“小结”和“ ...

  9. B-Tree、B+Tree和B*Tree

    B-Tree(这儿可不是减号,就是常规意义的BTree) 是一种多路搜索树: 1.定义任意非叶子结点最多只有M个儿子:且M>2: 2.根结点的儿子数为[2, M]: 3.除根结点以外的非叶子结点 ...

  10. <javascript搞基程序设计>笔记2015-9-25

    1.一元加减 var a=28; alert(--a); //27,先减后用 alert(a--); //27,先用后减 alert(a); //26, 2.位操作符 按位非(NOT):按位取反:符号 ...