最近有人问NSInvocation怎么用,于是写了个NSInvocation的小例子
来源:互联网 发布:elk windows 搭建 编辑:程序博客网 时间:2024/04/28 02:46
直接上代码
main.h
#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
#import <Foundation/Foundation.h>@interface MyClass : NSObject { }- (NSString *)appendMyString:(NSString *)string;@end
MyClass.m
#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的小例子
- 最近有人问NSInvocation怎么用,于是写了个NSInvocation的小例子
- NSInvocation 的使用小例子
- NSInvocation
- NSInvocation
- NSInvocation
- NSInvocation
- NSInvocation
- NSInvocation
- NSInvocation
- NSInvocation
- NSInvocation的使用
- NSInvocation的使用
- NSInvocation的一种用法
- NSInvocation的用法
- NSInvocation的使用
- NSInvocation的使用
- NSInvocation的使用
- 完整的Oracle数据库通常由两部分组成:实例和数据库。
- 《五天学会绘画》阅读感受(全,附绘画作品)
- Building Coder(Revit 二次开发)- 在新建族中进行镜像操作,并切换当前视图
- 数据排序(排序小结)
- POJ 3468 A SIMPLE PROBLEM WITH INTEGERS(线段树)
- 最近有人问NSInvocation怎么用,于是写了个NSInvocation的小例子
- Android:处理触摸事件
- 常用数据库代码(数据库)
- oracle 笔记7(Fundamentals II)
- [Lua脚本实践1]LuaPlus中关于语句的执行
- POJ 3321 APPLE TREE(树状数组+DFS)
- [Python源码学习]之Very High Level Layer
- MyEclipse使用SVN插件报Failed to load JavaHL Library.错误
- Linux 下的三款 Flash 独立播放器