前言
在 iOS中可以直接調(diào)用某個對象的消息方式有兩種:
一種是performSelector:withObject牵触;
再一種就是NSInvocation拍埠。
第一種方式比較簡單,能完成簡單的調(diào)用膳沽。但是對于>2個的參數(shù)或者有返回值的處理,那performSelector:withObject就顯得有點有心無力了添诉,那么在這種情況下,我們就可以使用NSInvocation來進(jìn)行這些相對復(fù)雜的操作
NSInvocation的基本使用
方法簽名類
// 方法簽名中保存了方法的名稱/參數(shù)/返回值医寿,協(xié)同NSInvocation來進(jìn)行消息的轉(zhuǎn)發(fā)
// 方法簽名一般是用來設(shè)置參數(shù)和獲取返回值的, 和方法的調(diào)用沒有太大的關(guān)系
//1栏赴、根據(jù)方法來初始化NSMethodSignature
NSMethodSignature *signature = [ViewController instanceMethodSignatureForSelector:@selector(run:)];
根據(jù)方法簽名來創(chuàng)建NSInvocation對象
- (void)viewDidLoad {
[super viewDidLoad];
//方法
SEL selector = @selector(run);
//初始化方法簽名(方法的描述)
NSMethodSignature *signature = [[self class] instanceMethodSignatureForSelector:selector];
// NSInvocation : 利用一個NSInvocation對象包裝一次方法調(diào)用(方法調(diào)用者、方法名靖秩、方法參數(shù)须眷、方法返回值)
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:signature];
//設(shè)置調(diào)用者
invocation.target = self;
//設(shè)置調(diào)用方法
invocation.selector = selector;
//設(shè)置參數(shù)
NSUInteger object = 5;
//參數(shù)從2開始,index為0表示target盆偿,1為_cmd
[invocation setArgument:&object atIndex:2];
//調(diào)用方法
[invocation invoke];
}
-(void)run:(NSInteger)num{
NSLog(@"run");
}
NSInvocation實現(xiàn)多參數(shù)的封裝
系統(tǒng)的NSObject提供的performSelector的方法只提供了最多兩個參數(shù)的調(diào)用,我們可以使用NSInvocation封裝一個多個參數(shù)的performSelector方法.
#import <Foundation/Foundation.h>
@interface NSObject (MutiPerform)
-(id)performSelector:(SEL)Selector withObjects:(NSArray *)objects;
@end
#import "NSObject+MutiPerform.h"
@implementation NSObject (MutiPerform)
-(id)performSelector:(SEL)selector withObjects:(NSArray *)objects{
//初始化方法簽名
NSMethodSignature *signature = [[self class] instanceMethodSignatureForSelector:selector];
// 如果方法selector不存在
if(signature == nil){
// 拋出異常
NSString *reason = [NSString stringWithFormat:@"%@方法不存在",NSStringFromSelector(selector)];
@throw [NSException exceptionWithName:@"error" reason:reason userInfo:nil];
}
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:signature];
invocation.target = self;
invocation.selector = selector;
//參數(shù)個數(shù)signature.numberOfArguments 默認(rèn)有一個_cmd 一個target 所以要-2
NSInteger paramsCount = signature.numberOfArguments - 2;
// 當(dāng)objects的個數(shù)多于函數(shù)的參數(shù)的時候,取前面的參數(shù)
//當(dāng)當(dāng)objects的個數(shù)少于函數(shù)的參數(shù)的時候,不需要設(shè)置,默認(rèn)為nil
paramsCount = MIN(paramsCount, objects.count);
for (NSInteger index = 0; index < paramsCount; index++) {
id object = objects[index];
// 對參數(shù)是nil的處理
if([object isKindOfClass:[NSNull class]]) continue;
[invocation setArgument:&object atIndex:index+2];
}
//調(diào)用方法
[invocation invoke];
// 獲取返回值
id returnValue = nil;
//signature.methodReturnLength == 0 說明給方法沒有返回值
if (signature.methodReturnLength) {
//獲取返回值
[invocation getReturnValue:&returnValue];
}
//可以通過signature.methodReturnType獲得返回的類型編碼,因此可以推斷返回值的具體類型
return returnValue;
}