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
@interface CurrentDate : NSObject {
}
- (NSString * ) stringForDate: (NSDate * )date
usingFormatter: (NSDateFormatter * )formatter;
@end
CurrentDate.m
@implementation CurrentDate
- (NSString * ) stringForDate: (NSDate * )date
usingFormatter: (NSDateFormatter * )formatter
{
return [formatter stringFromDate: date];
}
@end
main.m
#import " CurrentDate.h "
// 參考: http://theocacao.com/document.page/264
int main ( int argc, const char * 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];
return 0 ;
}
http://www.j2megame.org/index.php/content/view/2635/165.html
NSMethodSignature和NSInvocation的使用
動態調用方法時會用到,例子 另外一個例子:
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 ] |