几个概念
- 方法调用的本质,就是让对象发送消息。
-
NSString *idfa = [Device getIdfa:type];
该方法调用的时会转化为objc_msgSend
进行调用,getIdfa:
以及后面的参数形成了一个消息。 - 看一下
objc_msgSend
这个方法的系统定义,void objc_msgSend(void /* id self, SEL op, ... */ )
,消息发送最少包括两个参数,一个id
类型,一个SEL
类型。 - 当对象对应方法列表中查询不到这个方法时,并不做任何处理的情况下,系统就会抛出异常。
消息发送到最后对应到方法实现的查找过程:SEL->Method->IMP,最后利用IMP函数指针调用方法,流程图如下:
- 应用场景:随意调用所有的方法,并且为实现不会崩溃。
在我们开发中,有些方法我们不愿意在.h中公开,导入某些头文件一不注意就会循环引用。并且很多时候会造成代码的耦合性上升。
注意: - 使用
objc_msgSend
这个方法的前提是你需要在BuildSetting
中设置Enable Strict Checking of objc_msgSend Calls
为YES
,否则会报错。 - 不要以为导入了头文件
#import<objc/runtime.h>
就完事了,同时记得导入#import<objc/message.h>
。
举个🌰
首先创建一个Person
类,.h中不公开任何方法,在.m中实现如下方法:
/**
*实例方法 无参数 无返回值
*/
-(void)eat{
NSLog(@"eat_person");
}
/**
*类方法 无参数 无返回值
*/
+(void)eat{
NSLog("eat_calss");
}
/**
*睡觉实例方法 有参数 无返回值
*/
-(void)sleepOfHour:(NSNumber*)hour{
NSLog(@"sleep_person_%@",hour);
}
/**
*睡觉类方法 有参数 无返回值
*/
+(void)sleepOfHour:(NSNumber*)hour{
NSLog(@"sleep_class_%@",hour);
}
/**
*是否吃饱实例方法 有参数 有返回值
*/
-(NSNumber*)eatEnough:(NSNumber*)breadCount{
NSLog(@"breadCount_person_%@",breadCount);
return @(1);
}
/**
*是否吃饱类方法 有参数 有返回值
*/
+(NSNumber*)eatEnough:(NSNumber*)breadCount{
NSLog(@"breadCount_class_%@",breadCount);
return @(0);
}
runtime方法封装:
.h
#parama mark - objc_msgSend(最多五个参数)
/**
*实例方法
*/
+(id)msgSendToObj:(id)obj Selector:(SEL)selector Parms:(NSArray*)params NeedReturn:(BOOL)needReturn;
/**
*类方法
*/
+(id)msgSendToClass:(Class)YSClass Selector:(SEL)selector Prarms:(NSArray*)params NeedReturn:(BOOL)needReturn;
-----------------------------------------------------------
.m
#paragma mark - objc_msgSend
+(id)msgSendToObj:(id)obj Selector:(SEL)selector Prarms:(NSArray*)params NeedReturn:(BOOL)needReturn{
id returnValue = nil;
NSInteger paramsCount = params.count;
NSMutableArray *params_M = [NSMutableArray arrayWithArray:params];
//
while (params_M.count < 5) {
[params_M addObject:@""];
}
params = params_M;
//
if (obj && selector && [obj respondsToSelector:selector] && paramsCount <= 5) {
if (needReturn) {
returnValue = ((id (*) (id, SEL, id, id , id, id, id)) (void *)objc_msgSend) (obj, selector, params[0], params[1], params[2], params[3], params[4]);
}else{
((void (*) (id, SEL, id, id , id, id, id)) (void *)objc_msgSend)(obj, selector, params[0], params[1], params[2], params[3], params[4]);
}
}
return returnValue;
}
+(id)msgSendToClass:(Class)YSClass Selector:(SEL)selector Prarms:(NSArray*)params NeedReturn:(BOOL)needReturn{
id returnValue = nil;
NSInteger paramsCount = params.count;
NSMutableArray *params_M = [NSMutableArray arrayWithArray:params];
//
while (params_M.count < 5) {
[params_M addObject:@""];
}
params = params_M;
//
Method method = class_getClassMethod(YSClass, selector);
//
if (YSClass && selector && (int)method != 0 && paramsCount <= 5) {
if (needReturn) {
returnValue = ((id (*) (id, SEL, id, id , id, id, id)) (void *)objc_msgSend) (YSClass, selector, params[0], params[1], params[2], params[3], params[4]);
}else{
((void (*) (id, SEL, id, id , id, id, id)) (void *)objc_msgSend)(YSClass, selector, params[0], params[1], params[2], params[3], params[4]);
}
}
return returnValue;
}
在工程中调用方法如下:
Person* person = [[Person alloc] init];
[NSObject msgSendToObj:person Selector:NSSelectorFromString(@"eat") Prarms:nil NeedReturn:NO];
[NSObject msgSendToClass:NSClassFromString(@"Person") Selector:NSSelectorFromString(@"eat") Prarms:nil NeedReturn:NO];
//
[NSObject msgSendToObj:person Selector:NSSelectorFromString(@"sleepOfHour:") Prarms:@[@(10)] NeedReturn:NO];
[NSObject msgSendToClass:NSClassFromString(@"Person") Selector:NSSelectorFromString(@"sleepOfHour:") Prarms:@[@(8)] NeedReturn:NO];
//
id objR = [NSObject msgSendToObj:person Selector:NSSelectorFromString(@"eatEnough:") Prarms:@[@(100)] NeedReturn:YES];
id classR = [NSObject msgSendToClass:NSClassFromString(@"Person") Selector:NSSelectorFromString(@"eatEnough:") Prarms:@[@(100)] NeedReturn:YES];
NSLog(@"objR_%@ classR_%@", objR, classR);
打印结果:
RuntimeSkill[2126:215490] eat_person
RuntimeSkill[2126:215490] eat_class
RuntimeSkill[2126:215490] sleep_person_10
RuntimeSkill[2126:215490] sleep_class_8
RuntimeSkill[2126:215490] breadCount_person_100
RuntimeSkill[2126:215490] breadCount_class_100
RuntimeSkill[2126:215490] objR_1 classR_0
有了限制参数个数的,当然也有不限制参数个数的---NSInvocation
.h
#pragma mark - NSInvocation (不限参数)
/**
*实例方法
*/
+(id)msgSendToObj_invocation:(id)obj Selector:(SEL)selector Prarms:(NSArray*)params NeedReturn:(BOOL)needReturn;
/**
*类方法
*/
+(id)msgSendToClass_invocation:(Class)YSClass Selector:(SEL)selector Prarms:(NSArray*)params NeedReturn:(BOOL)needReturn;
------------------------------------------------------------------
.m
#pragma mark - NSInvocation (不限参数)
+(id)msgSendToObj_invocation:(id)obj Selector:(SEL)selector Prarms:(NSArray*)params NeedReturn:(BOOL)needReturn{
id value = nil;
if (obj && selector) {
if ([obj respondsToSelector:selector]) {
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[[obj class] instanceMethodSignatureForSelector:selector]];
[invocation setSelector:selector];
[invocation setTarget:obj];
for (int i=0; i < params.count; i++) {
id ref = params[i];
[invocation setArgument:&ref atIndex:2+i];
}
[invocation invoke];//perform 的传参表达方式
if(needReturn){//获得返回值
void *vvl = nil;
[invocation getReturnValue:&vvl];
value = (__bridge id)vvl;
}
}else{
#ifdef _YSDebugLog
NSLog(@"msgToTarget unRespondsToSelector -->>> %@ %@",obj,menthed);
#endif
}
}
return value;
}
+(id)msgSendToClass_invocation:(Class)YSClass Selector:(SEL)selector Prarms:(NSArray*)params NeedReturn:(BOOL)needReturn{
id value = nil;
Method method = class_getClassMethod(YSClass, selector);
if((int)method != 0){
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[YSClass methodSignatureForSelector:selector]];
[invocation setSelector:selector];
[invocation setTarget:YSClass];
for (int i=0; i < params.count; i++) {
id ref = params[i];
[invocation setArgument:&ref atIndex:2+i];
}
[invocation invoke];//perform 的传参表达方式
if(needReturn){//获得返回值
void *vvl = nil;
[invocation getReturnValue:&vvl];
value = (__bridge id)vvl;
}
}else{
#ifdef _YSDebugLog
NSLog(@"msgToClass unRespondsToSelector -->>> %@ %@",YSClass,menthed);
#endif
}
return value;
}
调用方法:
[NSObject msgSendToObj_invocation:person Selector:NSSelectorFromString(@"eat") Prarms:nil NeedReturn:NO];
[NSObject msgSendToClass_invocation:NSClassFromString(@"Person") Selector:NSSelectorFromString(@"eat") Prarms:nil NeedReturn:NO];
//
[NSObject msgSendToObj_invocation:person Selector:NSSelectorFromString(@"sleepOfHour:") Prarms:@[@(10)] NeedReturn:NO];
[NSObject msgSendToClass_invocation:NSClassFromString(@"Person") Selector:NSSelectorFromString(@"sleepOfHour:") Prarms:@[@(8)] NeedReturn:NO];
//
id objR_in = [NSObject msgSendToObj_invocation:person Selector:NSSelectorFromString(@"eatEnough:") Prarms:@[@(100)] NeedReturn:YES];
id classR_in = [NSObject msgSendToClass_invocation:NSClassFromString(@"Person") Selector:NSSelectorFromString(@"eatEnough:") Prarms:@[@(100)] NeedReturn:YES];
NSLog(@"objR_in_%@ classR_in_%@", objR_in, classR_in);
打印结果:
RuntimeSkill[2150:221988] eat_person
RuntimeSkill[2150:221988] eat_class
RuntimeSkill[2150:221988] sleep_person_10
RuntimeSkill[2150:221988] sleep_class_8
RuntimeSkill[2150:221988] breadCount_person_100
RuntimeSkill[2150:221988] breadCount_class_100
RuntimeSkill[2150:221988] objR_in_1 classR_in_0
写在最后:
Runtime在工作中有用到,但是没有系统的整理过 这些天看到有博文整理了,就借来用用,方便以后查阅。
网友评论