您的位置:首页 > 移动开发 > IOS开发

iOS NSInvocation

2016-02-24 16:21 381 查看
苹果文档:

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. 

An 
NSInvocation
 object
contains all the elements of an Objective-C message: a target, a selector, arguments, and the return value. Each of these elements can be set directly, and the return value is set automatically when the 
NSInvocation
 object
is dispatched.

An 
NSInvocation
 object
can be repeatedly dispatched to different targets; its arguments can be modified between dispatch for varying results; even its selector can be changed to another with the same method signature (argument and return types). This flexibility makes 
NSInvocation
 useful
for repeating messages with many arguments and variations; rather than retyping a slightly different expression for each message, you modify the 
NSInvocation
 object
as needed each time before dispatching it to a new target.

NSInvocation
 does
not support invocations of methods with either variable numbers of arguments or 
union
arguments.
You should use the 
invocationWithMethodSignature:
 class
method to create 
NSInvocation
objects;
you should not create these objects using 
alloc
 and 
init
.

This class does not retain the arguments for the contained invocation by default. If those objects might disappear between the time you create your instance of 
NSInvocation
 and
the time you use it, you should explicitly retain the objects yourself or invoke the 
retainArguments
 method
to have the invocation object retain them itself.



Getting the Method Signature

methodSignature
 Property
 



   Creating NSInvocation Objects

   
 + invocationWithMethodSignature:




   Configuring an Invocation Object

 selector
 Property

 target
 Property

 
- setArgument:atIndex:


  
- getArgument:atIndex:


argumentsRetained
 Property

  
- retainArguments 它会将传入的所有参数以及target都retain一遍。


 
- setReturnValue:


   
- getReturnValue:




Dispatching an Invocation

  
- invoke


  
- invokeWithTarget:

简单例子:
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: