iOS NSInvocation用法简介

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

一种是performSelector:withObject;

再一种就是NSInvocation。

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

//test performSelector      注意:此种方法只使用于最多两个参数,多个参数无能为力
- (void)testMutiParams {
   
//test perform
   
NSString *strRe = [self performSelector:@selector(appendString:String2:) withObject:@"hello" withObject:@"world"];
   
NSLog(@"strRe :%@",strRe);
   
   
//question ?
   
NSString *strRe1 = [self performSelector:@selector(appendString:String2:String3:) withObject:@"hello" withObject:@"world"];
   
NSLog(@"strRe :%@",strRe1);

   
NSString *strMy1 = @"My string1";
   
NSString *strMy2 = @"My string2";
   
NSString *strMy3 = @"My string3";
   
   
SEL mySelector = @selector(appendString:String2:String3:);
   
NSMethodSignature *sig = [[self class]instanceMethodSignatureForSelector:mySelector];
   
   
NSInvocation *myInvocation = [NSInvocation invocationWithMethodSignature:sig];
    [myInvocation
setTarget:self];
    [myInvocation
setSelector:mySelector];
    [myInvocation
setArgument:&strMy1 atIndex:2];
    [myInvocation
setArgument:&strMy2 atIndex:3];
    [myInvocation
setArgument:&strMy3 atIndex:4];
   
   
NSString *strResult = nil;
    [myInvocation
retainArguments];
    [myInvocation
invoke];
    [myInvocation
getReturnValue:&strResult];
   
NSLog(@"The NSInvocation invoke string is :%@",strResult);

}

- (NSString *)appendString:(NSString *)str1 String2:(NSString *)str2 String3:(NSString *)str3 {
   
return [NSString stringWithFormat:@"%@,%@,%@",str1,str2,str3];
}







- (void)testInvocation {
    MyClass *myClass = [[MyClass alloc] init];
   
NSString *strMy = @"My string";
   
   
//common selector
   
NSString *normalInvokeString = [myClass appendMyString:strMy];
   
NSLog(@"The normal invoke string is :%@",normalInvokeString);
   
   
//NSInvocation selector
   
SEL mySelector = @selector(appendMyString:);
   
NSMethodSignature *sig = [[myClass class]instanceMethodSignatureForSelector:mySelector];
   
   
NSInvocation *myInvocation = [NSInvocation invocationWithMethodSignature:sig];
    [myInvocation
setTarget:myClass];
    [myInvocation
setSelector:mySelector];
    [myInvocation
setArgument:&strMy atIndex:2];
   
   
NSString *strResult = nil;
    [myInvocation
retainArguments];
    [myInvocation
invoke];
    [myInvocation
getReturnValue:&strResult];
   
NSLog(@"The NSInvocation invoke string is :%@",strResult);
 

}

类实现:

#import <Foundation/Foundation.h>

@interface MyClass : NSObject

- (
NSString *)appendMyString:(NSString *)string;

@end

#import "MyClass.h"

@implementation MyClass

- (
NSString *)appendMyString:(NSString *)string {
   
NSString *mString = [NSString stringWithFormat:@"%@ after append method", string];
   
return mString;
}

@end


这里说明一下[myInvocation setArgument: &myString atIndex: 2];为什么index从2开始 ,原因为:0 1 两个参数已经被target 和selector占用。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值