博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Invocation
阅读量:6882 次
发布时间:2019-06-27

本文共 3175 字,大约阅读时间需要 10 分钟。

The NSInvocation class is designed to represent an Objective-C message. NSInovation instances encapsulates all the attributes of an Objective-C message. They know the message's receive, the message name (both the selector and method signature), and all the message's arguments. After invoking an NSInvocation, the message's return value may be retrieved from the NSInvocations.

One point of confusion for many developers is how messages are named. Each name has two parts, the selector and the method signature. A selector is the message's name without any type information, for example "countOfObjectsOfType:" is a selector. To build a complete message, the types of each argument and return value's type need to be known. This type information is known as a method signature. The class NSMethodSignature encapsulates this information.

// if you must use an id type, then use a cast when sending the messageid myObject;value = [(ClassA *)myObject countOfObjectsOfType:aType];// otherwise, use static typing instead of a generic id to disambiguateClassA *myObject;value = [myObject countOfObjectsOfType:aType];

At runtime, however, the receiver is known, and it is possible to simply ask it for the correct message signature, like this:

NSMethodSignature* mySignature = [myObjectmethodSignatureForSelector:@selector(countOfObjectsOfType:)];

a sample,

NSString *receivingString = [receiver stringValue];NSString *messageString = [message titleOfSelectedItem];SEL selector = NSSelectorFromString(messageString);NSMethodSignature *methodSignature = [receivingStringmethodSignatureForSelector:selector];NSInvocation *invocation = [NSInvocationinvocationWithMethodSignature:methodSignature];

Every Objective-C method has two hidden arguments. The first and most commonly used arguments is self. The second, containing the selector that invoked the method, is called _cmd.

[invocation setTarget:receivingString]; // argument 0 is "self"[invocation setSelector:selector]; // argument 1 is "_cmd"

Returning to the example, the next step is to configure the arguments for the invocation.

The -setArgument:atIndex: method of NSInvocation is used for this.The messages
being sent can have 0, 1, or 2 arguments, and the tag of the selected pop-up button
item tells how many arguments to set up. Because the hidden arguments self and _cmd
take up spots 0 and 1 in the argument list, the first argument to the method is actually argument 2.Also when using the -setArgument:atIndex: method, pointers to object
pointers must be used instead of just a pointer to the object. Here’s the code:

int numberOfArguments = [[message selectedItem] tag];if (numberOfArguments > 0){NSString *argumentString1 = [argument1 stringValue];[invocation setArgument:&argumentString1 atIndex:2];if (numberOfArguments > 1){NSString *argumentString2 = [argument2 stringValue];[invocation setArgument:&argumentString2 atIndex:3];}}

Here is the code to invoke the message and interpret the return value:

[invocation invoke];void *returnValue = NULL;[invocation getReturnValue:&returnValue];const char *returnType = [methodSignature methodReturnType];

 

 

 

 

 

 

转载于:https://www.cnblogs.com/grep/archive/2012/07/08/2581545.html

你可能感兴趣的文章
MEF简单学习笔记
查看>>
Srping - bean的依赖注入(Dependency injection)
查看>>
NSAutoreleasePool 用处
查看>>
import matplotlib.pyplot as plt出错
查看>>
常用集合与Dictionary用例
查看>>
MVC
查看>>
AI - TensorFlow - 张量(Tensor)
查看>>
js table 导出 Excel
查看>>
AHSC DAY2总结
查看>>
java.lang.SecurityException: class "javax.servlet.FilterRegistration"(spark下maven)
查看>>
[Vue CLI 3] 配置解析之 css.extract
查看>>
Linux——信息采集(三)dmitry、路由跟踪命令tracerouter
查看>>
提取ipa里面的资源图片 png
查看>>
wxpython ItemContainer
查看>>
工作中 Oracle 常用数据字典集锦
查看>>
SFB 项目经验-12-为某上市企业的Skype for Business购买Godday证书
查看>>
[C#基础知识]专题十三:全面解析对象集合初始化器、匿名类型和隐式类型
查看>>
大数据虚拟化零起点-2基础运维第一步-环境规划和准备
查看>>
Skype for Business Server 2015-04-前端服务器-3-安装-管理工具
查看>>
python 学习笔记 表达式(9)
查看>>