最近有人问NSInvocation怎么用,于是写了个NSInvocation的小例子
2012-10-15 16:45
232 查看
转自:http://blog.csdn.net/volcan1987/article/details/6690208
直接上代码
main.h
[html] view
plaincopy
#import <Foundation/Foundation.h>
#import "MyClass.h"
int main (int argc, const char * argv[])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
MyClass *myClass = [[MyClass alloc] init];
NSString *myString = @"My string";
//普通调用
NSString *normalInvokeString = [myClass appendMyString:myString];
NSLog(@"The normal invoke string is: %@", normalInvokeString);
//NSInvocation调用
SEL mySelector = @selector(appendMyString:);
NSMethodSignature * sig = [[myClass class]
instanceMethodSignatureForSelector: mySelector];
NSInvocation * myInvocation = [NSInvocation invocationWithMethodSignature: sig];
[myInvocation setTarget: myClass];
[myInvocation setSelector: mySelector];
[myInvocation setArgument: &myString atIndex: 2];
NSString * result = nil;
[myInvocation retainArguments];
[myInvocation invoke];
[myInvocation getReturnValue: &result];
NSLog(@"The NSInvocation invoke string is: %@", result);
[myClass release];
[pool drain];
return 0;
}
MyClass.h
[html] view
plaincopy
#import <Foundation/Foundation.h>
@interface MyClass : NSObject {
}
- (NSString *)appendMyString:(NSString *)string;
@end
MyClass.m
[html] view
plaincopy
#import "MyClass.h"
@implementation MyClass
- (id)init
{
self = [super init];
if (self) {
// Initialization code here.
}
return self;
}
- (NSString *)appendMyString:(NSString *)string
{
NSString *mString = [NSString stringWithFormat:@"%@ after append method", string];
return mString;
}
- (void)dealloc
{
[super dealloc];
}
@end
这里说明一下[myInvocation setArgument: &myString atIndex: 2];为什么index从2开始
文档中的说明
Indices 0 and 1 indicate the hidden arguments self and _cmd, respectively; you should set these values directly with the setTarget: and setSelector: methods. Use indices 2 and greater for the arguments normally passed in a message.意思就是0和1是隐藏参数,而这两个参数是要在setTarget和setSelector设置的,所以我们调用方法中的参数就要从2开始了,如果有多个参数,那么就依次递增,ok,就写这么多
直接上代码
main.h
[html] view
plaincopy
#import <Foundation/Foundation.h>
#import "MyClass.h"
int main (int argc, const char * argv[])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
MyClass *myClass = [[MyClass alloc] init];
NSString *myString = @"My string";
//普通调用
NSString *normalInvokeString = [myClass appendMyString:myString];
NSLog(@"The normal invoke string is: %@", normalInvokeString);
//NSInvocation调用
SEL mySelector = @selector(appendMyString:);
NSMethodSignature * sig = [[myClass class]
instanceMethodSignatureForSelector: mySelector];
NSInvocation * myInvocation = [NSInvocation invocationWithMethodSignature: sig];
[myInvocation setTarget: myClass];
[myInvocation setSelector: mySelector];
[myInvocation setArgument: &myString atIndex: 2];
NSString * result = nil;
[myInvocation retainArguments];
[myInvocation invoke];
[myInvocation getReturnValue: &result];
NSLog(@"The NSInvocation invoke string is: %@", result);
[myClass release];
[pool drain];
return 0;
}
MyClass.h
[html] view
plaincopy
#import <Foundation/Foundation.h>
@interface MyClass : NSObject {
}
- (NSString *)appendMyString:(NSString *)string;
@end
MyClass.m
[html] view
plaincopy
#import "MyClass.h"
@implementation MyClass
- (id)init
{
self = [super init];
if (self) {
// Initialization code here.
}
return self;
}
- (NSString *)appendMyString:(NSString *)string
{
NSString *mString = [NSString stringWithFormat:@"%@ after append method", string];
return mString;
}
- (void)dealloc
{
[super dealloc];
}
@end
这里说明一下[myInvocation setArgument: &myString atIndex: 2];为什么index从2开始
文档中的说明
Indices 0 and 1 indicate the hidden arguments self and _cmd, respectively; you should set these values directly with the setTarget: and setSelector: methods. Use indices 2 and greater for the arguments normally passed in a message.意思就是0和1是隐藏参数,而这两个参数是要在setTarget和setSelector设置的,所以我们调用方法中的参数就要从2开始了,如果有多个参数,那么就依次递增,ok,就写这么多
相关文章推荐
- 最近有人问NSInvocation怎么用,于是写了个NSInvocation的小例子
- 前两天有人问怎么取得一个目录下的文件,下面就是例子
- 不允许常规上传时。。(当站点有人检测过 怎么查看马儿的内容)
- 有人愿意分享下编程的心得吗?怎么增强自己的编程能力? ZZ
- Python入门的36个例子——09 While —— 怎么老是你
- 最近很迷茫-不知道该怎么办
- 听说在广州又有人被抢劫了,于是在sohu的首页上出现了他的一篇文章:广州是最安全的城市,全国人都笑了
- 最近学习的linux命令 grep,awk,cut,uniq,wc 实际应用例子
- 最近学习了如何以oracle的sys用户怎么通过jdbc连接
- 正被停用的激活上下文不是最近激活的 怎么办
- 竟然又有人特意加我QQ,问我,Eclipse里面怎么显示行号啊!
- 怎么google也不能访问了,最近网络搞什么啊?
- 小心主题为:“最近好吗?怎么联系不上你”的带毒邮件
- 怎么让Sublime Text不自动打开最近的文件/项目
- 有人很好奇我博客文章的默认展图是怎么弄的?
- 搞不懂怎么会有人喜欢超女?
- NSInvocation 的使用小例子
- 有人找你一夜晴,你会怎么办?
- win7 开始 菜单 自定义 要显示的最近打开过的程序的数目 是灰色的 不可以更改 怎么解决?
- 最近有人在网上骂柠檬