前言
在消息轉(zhuǎn)發(fā)中提到過NSInvocation
這個類,這里說一下我所理解的NSInvocation
宅荤。NSInvocation
是命令模式
的一種實現(xiàn)屑迂,它包含選擇器、方法簽名冯键、相應(yīng)的參數(shù)以及目標對象惹盼。所謂的方法簽名,即方法所對應(yīng)的返回值類型和參數(shù)類型惫确。當NSInvocation
被調(diào)用手报,它會在運行時通過目標對象去尋找對應(yīng)的方法蚯舱,從而確保唯一性,可以用[receiver message]
來解釋掩蛤。實際開發(fā)過程中直接創(chuàng)建NSInvocation
的情況不多見枉昏,這些事情通常交給系統(tǒng)來做。比如bang
的JSPatch
中arm64
方法替換的實現(xiàn)就是利用runtime
消息轉(zhuǎn)發(fā)最后一步中的NSInvocation
實現(xiàn)的揍鸟。
正文
基于這種命令模式兄裂,可以利用NSInvocation
調(diào)用任意SEL
甚至block
。
SEL
- (id)performSelector:(SEL)aSelector withArguments:(NSArray *)arguments {
if (aSelector == nil) return nil;
NSMethodSignature *signature = [[self class] instanceMethodSignatureForSelector:aSelector];
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:signature];
invocation.target = self;
invocation.selector = aSelector;
// invocation 有2個隱藏參數(shù)阳藻,所以 argument 從2開始
if ([arguments isKindOfClass:[NSArray class]]) {
NSInteger count = MIN(arguments.count, signature.numberOfArguments - 2);
for (int i = 0; i < count; i++) {
const char *type = [signature getArgumentTypeAtIndex:2 + i];
// 需要做參數(shù)類型判斷然后解析成對應(yīng)類型晰奖,這里默認所有參數(shù)均為OC對象
if (strcmp(type, "@") == 0) {
id argument = arguments[i];
[invocation setArgument:&argument atIndex:2 + i];
}
}
}
[invocation invoke];
id returnVal;
if (strcmp(signature.methodReturnType, "@") == 0) {
[invocation getReturnValue:&returnVal];
}
// 需要做返回類型判斷。比如返回值為常量需要包裝成對象腥泥,這里僅以最簡單的`@`為例
return returnVal;
}
與NSObject
中的performSelector
相比匾南,沒有了參數(shù)個數(shù)限制。
block
static id invokeBlock(id block ,NSArray *arguments) {
if (block == nil) return nil;
id target = [block copy];
const char *_Block_signature(void *);
const char *signature = _Block_signature((__bridge void *)target);
NSMethodSignature *methodSignature = [NSMethodSignature signatureWithObjCTypes:signature];
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:methodSignature];
invocation.target = target;
// invocation 有1個隱藏參數(shù)道川,所以 argument 從1開始
if ([arguments isKindOfClass:[NSArray class]]) {
NSInteger count = MIN(arguments.count, methodSignature.numberOfArguments - 1);
for (int i = 0; i < count; i++) {
const char *type = [methodSignature getArgumentTypeAtIndex:1 + i];
NSString *typeStr = [NSString stringWithUTF8String:type];
if ([typeStr containsString:@"\""]) {
type = [typeStr substringToIndex:1].UTF8String;
}
// 需要做參數(shù)類型判斷然后解析成對應(yīng)類型午衰,這里默認所有參數(shù)均為OC對象
if (strcmp(type, "@") == 0) {
id argument = arguments[i];
[invocation setArgument:&argument atIndex:1 + i];
}
}
}
[invocation invoke];
id returnVal;
const char *type = methodSignature.methodReturnType;
NSString *returnType = [NSString stringWithUTF8String:type];
if ([returnType containsString:@"\""]) {
type = [returnType substringToIndex:1].UTF8String;
}
if (strcmp(type, "@") == 0) {
[invocation getReturnValue:&returnVal];
}
// 需要做返回類型判斷。比如返回值為常量需要包裝成對象冒萄,這里僅以最簡單的`@`為例
return returnVal;
}
SEL與block比較
- invocation
SEL既有target也有selector臊岸,block只有target - signature
SEL有兩個隱藏參數(shù),類型均為類型為@
@
和:
尊流,分別對應(yīng)target和selector帅戒。block有一個隱藏參數(shù),類型為@?
崖技,對應(yīng)target且block的target為他本身 - type
以O(shè)C對象為例:SEL的type為@
逻住,block的type會跟上具體類型,如@"NSString"
再談block
在block的invocation中有這樣的代碼
const char *_Block_signature(void *);
const char *signature = _Block_signature((__bridge void *)target);
_Block_signature
其實是JavaScriptCore/ObjcRuntimeExtras.h
中的私有API(這個頭文件并沒有公開可以戳這里查看)
既然蘋果把API封了迎献,那就自己實現(xiàn)咯瞎访,萬能的github早有答案CTObjectiveCRuntimeAdditions
把CTBlockDescription.h
和CTBlockDescription.m
拖到項目中,代碼這樣寫
static id invokeBlock(id block ,NSArray *arguments) {
if (block == nil) return nil;
id target = [block copy];
CTBlockDescription *ct = [[CTBlockDescription alloc] initWithBlock:target];
NSMethodSignature *methodSignature = ct.blockSignature;
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:methodSignature];
invocation.target = target;
// invocation 有1個隱藏參數(shù)吁恍,所以 argument 從1開始
if ([arguments isKindOfClass:[NSArray class]]) {
NSInteger count = MIN(arguments.count, methodSignature.numberOfArguments - 1);
for (int i = 0; i < count; i++) {
const char *type = [methodSignature getArgumentTypeAtIndex:1 + i];
NSString *typeStr = [NSString stringWithUTF8String:type];
if ([typeStr containsString:@"\""]) {
type = [typeStr substringToIndex:1].UTF8String;
}
// 需要做參數(shù)類型判斷然后解析成對應(yīng)類型扒秸,這里默認所有參數(shù)均為OC對象
if (strcmp(type, "@") == 0) {
id argument = arguments[i];
[invocation setArgument:&argument atIndex:1 + i];
}
}
}
[invocation invoke];
id returnVal;
const char *type = methodSignature.methodReturnType;
NSString *returnType = [NSString stringWithUTF8String:type];
if ([returnType containsString:@"\""]) {
type = [returnType substringToIndex:1].UTF8String;
}
if (strcmp(type, "@") == 0) {
[invocation getReturnValue:&returnVal];
}
// 需要做返回類型判斷。比如返回值為常量需要包裝成對象冀瓦,這里僅以最簡單的`@`為例
return returnVal;
}