objc_msgSend

2017-07-18  本文已影响69人  无题007

几个概念

359DC26A-2609-4E48-AF8B-C56DC4CB1984.png

举个🌰

首先创建一个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在工作中有用到,但是没有系统的整理过 这些天看到有博文整理了,就借来用用,方便以后查阅。

上一篇 下一篇

猜你喜欢

热点阅读