转载自:http://www.cnblogs.com/pengyingh/articles/2359199.html

http://blog.iosxcode4.com/?p=125

在 iOS中可以直接调用 某个对象的消息 方式有2种

一种是performSelector:withObject:

再一种就是NSInvocation

第一种方式比较简单,能完成简单的调用。但是对于>2个的参数或者有返回值的处理,那就需要做些额外工作才能搞定。那么在这种情况下,我们就可以使用NSInvocation来进行这些相对复杂的操作

NSInvocation可以处理参数、返回值。会java的人都知道反射操作,其实NSInvocation就相当于反射操作。

下面这个例子描述了如何使用NSInvocation,以下例子中如果要正常运行,需要把不存在的类进行正确填写。

//方法签名类,需要被调用消息所属的类AsynInvoke ,被调用的消息invokeMethod:

NSMethodSignature *sig= [[AsynInvoke class] instanceMethodSignatureForSelector:@selector(invokeMethod:)];

//根据方法签名创建一个NSInvocation

NSInvocation *invocation=[NSInvocation invocationWithMethodSignature:sig];

//设置调用者也就是AsynInvoked的实例对象,在这里我用self替代

[invocation setTarget:self];

//设置被调用的消息

[invocation setSelector:@selector(invokeMethod:)];

//如果此消息有参数需要传入,那么就需要按照如下方法进行参数设置,需要注意的是,atIndex的下标必须从2开始。原因为:0 1 两个参数已经被target 和selector占用

NSInteger num=10;

[invocation setArgument:&num atIndex:2];

//retain 所有参数,防止参数被释放dealloc

[invocation retainArguments];

//消息调用

[invocation invoke];

//如果调用的消息有返回值,那么可进行以下处理

//获得返回值类型

const char *returnType = sig.methodReturnType;

//声明返回值变量

id returnValue;

//如果没有返回值,也就是消息声明为void,那么returnValue=nil

if( !strcmp(returnType, @encode(void)) ){

returnValue =  nil;

}

//如果返回值为对象,那么为变量赋值

else if( !strcmp(returnType, @encode(id)) ){

[invocation getReturnValue:&returnValue];

}

else{

//如果返回值为普通类型NSInteger  BOOL

//返回值长度

NSUInteger length = [sig methodReturnLength];

//根据长度申请内存

void *buffer = (void *)malloc(length);

//为变量赋值

[invocation getReturnValue:buffer];

if( !strcmp(returnType, @encode(BOOL)) ) {

returnValue = [NSNumber numberWithBool:*((BOOL*)buffer)];

}

else if( !strcmp(returnType, @encode(NSInteger)) ){

returnValue = [NSNumber numberWithInteger:*((NSInteger*)buffer)];

}

returnValue = [NSValue valueWithBytes:buffer objCType:returnType];

}

NSInvocation调用

CurrentDate.h


#import <Foundation/Foundation.h>

@interface CurrentDate : NSObject {

}
- (NSString *) stringForDate: (NSDate *)date 
usingFormatter: (NSDateFormatter *)formatter; @end
复制代码

CurrentDate.m


#import "CurrentDate.h"

@implementation CurrentDate

- (NSString *) stringForDate: (NSDate *)date 
usingFormatter: (NSDateFormatter *)formatter
{
return [formatter stringFromDate: date];
} @end
复制代码

main.m


#import <Foundation/Foundation.h>
#import "CurrentDate.h" //参考:http://theocacao.com/document.page/264
int main (int argc, constchar* argv[])
{ NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; //原始调用
NSDateFormatter * dateFormat = [[NSDateFormatter alloc] 
initWithDateFormat:@"%b %d %Y" 
allowNaturalLanguage: NO];
CurrentDate * currentDateClassObject = [[CurrentDate alloc] init];
NSString * currentDate = [currentDateClassObject 
stringForDate: [NSDate date] 
usingFormatter: dateFormat];
NSLog(@"currentDate: %@", currentDate); //NSInvocation调用
SEL mySelector = @selector(stringForDate:usingFormatter:);
NSMethodSignature * sig = [[currentDateClassObject class] 
instanceMethodSignatureForSelector: mySelector]; NSInvocation * myInvocation = [NSInvocation invocationWithMethodSignature: sig];
[myInvocation setTarget: currentDateClassObject];
[myInvocation setSelector: mySelector]; NSDate * myDate = [NSDate date];
[myInvocation setArgument: &myDate atIndex: 2]; NSDateFormatter * dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateStyle: NSDateFormatterMediumStyle]; 
[myInvocation setArgument: &dateFormatter atIndex: 3]; NSString * result = nil; 
[myInvocation retainArguments]; 
[myInvocation invoke];
[myInvocation getReturnValue: &result];
NSLog(@"The result is: %@", result); [pool drain];
return0;
}
复制代码

http://www.j2megame.org/index.php/content/view/2635/165.html

NSMethodSignature和NSInvocation的使用

动态调用方法时会用到,例子

-(NSString *)myMethod:(NSString *)param1 withParam2:(NSNumber *)param2 

    NSString *result = @"objc"; 
    NSLog(@"par = %@",param1); 
    NSLog(@"par 2 = %@",param2); 
    return result; 
}

-(void)invokeMyMethodDynamically 

    SEL selector = @selector(myMethod:withParam2:); 
    NSMethodSignature *methodSignature = [[self class] instanceMethodSignatureForSelector:selector];//获得类和方法的签名 
    NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:methodSignature]; 
    //从签名获得调用对象 
    [invocation setTarget:self]; 
    //设置target 
    [invocation setSelector:selector];//设置selector 
    NSString *returnValue = nil; 
    NSString *argument1 = @"fist"; 
    NSNumber *argument2 = [NSNumber numberWithInt:102]; 
    [invocation setArgument:&argument1 atIndex:2];//设置参数,第一个参数index为2 
    [invocation setArgument:&argument2 atIndex:3]; 
    [invocation retainArguments];//retain一遍参数 
    [invocation invoke];//调用 
    [invocation getReturnValue:&returnValue];//得到返回值,此时不会再调用,只是返回值 
    NSLog(@"return value = %@",returnValue); 
}

另外一个例子:

SEL selector = @selector(myMethod:setValue2:);

NSMethodSignature *signature = [MyObject instanceMethodSignatureF orSelector:selector];
NSInvocation *invocation = [NSInvocation invocationWithMethodSign ature:signature];
[invocation setSelector:selector];

NSString *str1 = @"someString";
NSString *str2 = @"someOtherString";

//The invocation object must retain its arguments
[str1 retain];
[str2 retain];

//Set the arguments
[invocation setTarget:targetInstance];
[invocation setArgument:&str1 atIndex:2];
[invocation setArgument:&str2 atIndex:3];

[NSTimer scheduledTimerWithTimeIn terval:0.1 invocation:invocation repeats:YES]

最新文章

  1. Chrome插件(Extensions)开发攻略
  2. Vue.js学习笔记(2)vue-router
  3. 如何使用.NET开发全版本支持的Outlook插件产品(三)&mdash;&mdash;全面控制
  4. haproxy配置文件
  5. T-SQL 常用语句
  6. IOS开发之路四(UITabBarController)
  7. 如何写robots.txt?
  8. 让footer在底部(测试它人方法)
  9. datalog
  10. IconFont --阿里巴巴矢量库
  11. 策略模式(Strategy Model)
  12. ajax跨域请求のJSONP
  13. QT学习相关
  14. Android studio 自己定义打包APK名称
  15. delphi locate函数的使用
  16. C#如何判断程序调用的exe已结束
  17. BZOJ4012 [HNOI2015]开店 (动态点分治)
  18. MapInfo 文件格式说明
  19. MySQL索引分析
  20. [BZOJ1821][JSOI2010]Group 部落划分 Group 最小生成树 贪心

热门文章

  1. Wing IDE编译TesorFlow中Mnist convolutional 实例
  2. WampServer下修改和重置MySQL密码(转)
  3. spark RDD的元素顺序(ordering)测试
  4. UML——综合实例
  5. Linux文件系统结构
  6. Xcode6模拟器时BUG导致键盘无法弹出
  7. minicsv库的编译错误与解决方案
  8. Bitmap、CBitmap、HBITMAP以及BITMAP的相互转换
  9. [GeekBand] STL与泛型编程(3)
  10. Qt学习总结-ui篇(二)