iOS Runtime之反射调用

2022-06-29  本文已影响0人  谢二九

Runtime系列导读

简介

笔者在做OpenAPI时需要对使用不同技术栈(H5、jscore、RN、小程序)的业务开放模块能力。为了做到最大能力复用,定义了统一的接口方式call,通过数据{"api":"","request":{}}来反射调用对应的Native方法。这里不讨论业务,仅针对反射调用的几种方式做一个总结。

方案

通过 performSelector

先看下performSelector的基础语法,从方法定义来看,他的入参类型和返回类型都要求是对象

// 判断是否有对应的selector
- (BOOL)respondsToSelector:(SEL)aSelector;

// 根据不同的参数来选择合适的方法,最多支持两个
- (id)performSelector:(SEL)aSelector;
- (id)performSelector:(SEL)aSelector withObject:(id)object;
- (id)performSelector:(SEL)aSelector withObject:(id)object1 withObject:(id)object2;

通过 objc_msgSend

OC方法调用的最终实现是调用了objc_msgSend(**void**/* id self, SEL op, ... */ ) 。所以我们可以直接用objc_msgSend来做方法调用。

SEL selector = @selector(testAdd:with:);
int returnValue = ((int (*)(id, SEL, int, int))
                 objc_msgSend)((id)self,
                               selector,
                               10,
                               20);
NSLog(@"value is %d", returnValue);

从上面例子可以看出,该方法最重要的是对调用信息的描述:(int (*)(id, SEL, int, int)),包含了返回类型,target对象,selector,以及对应的参数类型。

通过 invocation

先直接给出调用示例:

//根据方法创建签名对象sign
SEL selector = @selector(testAdd:with:);
NSMethodSignature *sign = [[self class] instanceMethodSignatureForSelector:selector];
//根据签名创建invocation
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:sign];
//设置调用对象相关信息
invocation.target = self;
invocation.selector = selector;
int a = 30;
int b = 10;
// 参数从index=2开始
[invocation setArgument:&a atIndex:2];
[invocation setArgument:&b atIndex:3];
//消息调用
[invocation invoke];

const char *returnType = sign.methodReturnType;
NSUInteger returnLength = sign.methodReturnLength;
if (!returnLength) {
    NSLog(@"无返回值");
}else if (!strcmp(returnType, @encode(id))) {
    __autoreleasing id returnValue = nil;
    [invocation getReturnValue:&returnValue];
    NSLog(@"对象:%@", returnValue);
}else{
    NSValue* returnValue = nil;
    void *buffer = (void *)malloc(returnLength);
    [invocation getReturnValue:buffer];
    //根据实际需要转换成对应的类型
    returnValue = [NSValue valueWithBytes:buffer objCType:returnType];
    NSLog(@"基础类型:%s", [returnValue objCType]);
}

这里做简单分析:

上一篇 下一篇

猜你喜欢

热点阅读