美文网首页
【iOS】performSelector详解(上篇)

【iOS】performSelector详解(上篇)

作者: iwakevin | 来源:发表于2019-06-03 11:39 被阅读0次

    转自公众号:NA分享

    performSelector简单使用

    iOS中提供了如下几种常用的调用方式:

    [self performSelector:@selector(sureTestMethod)];
    [self performSelector:@selector(sureTestMethod)
               withObject:params];
    [self performSelector:@selector(sureTestMethod)
               withObject:params
               withObject:params2];
    

    iOS动态调用

    performSelector可以向一个对象传递任何消息,而不需要在编译的时候声明这些方法,这也是runtime的一种应用方式,所以performSelector和直接调用方法的区别就在与runtime。直接调用编译是会自动校验。如果方法不存在,那么直接调用 在编译时候就能够发现,编译器会直接报错。 但是使用performSelector的话一定是在运行时候才能发现,如果此方法不存在就会崩溃。所以performSelector- (BOOL)respondsToSelector:(SEL)aSelector搭配使用,来在运行时判断对象是否响应此方法。因为此特性,performSelector也广泛用于动态化和组件化的模块中。

    SEL selector = @selector(dynamicMethod);
    [self performSelector:selector];
    

    如果方法名称也是动态不确定的,会提示如下警告:

    ⚠️ PerformSelector may cause a leak because its selector is unknown
    

    意为因为当前方法名未知可能会引起内存泄露相关问题。 可以通过如下代码忽略此警告

    #pragma clang diagnostic push
    #pragma clang diagnostic ignored "-Warc-performSelector-leaks"
        [self performSelector:selector];
    #pragma clang diagnostic pop
    

    performSelector默认最多只可传递两个参数,若需多参参考以下方式:
    一种是使用NSInvocation,利用了runtime的反射机制,效率较低,可读性不高;
    第二种是将参数封装进NSArrayNSDictionary等对象,可读性强,效率高;
    第三种是使用objc_msgSend重写performSelector

    第一种:NSInvocation

    - (id)performSelector:(SEL)selector withObjects:(NSArray *)objects
    {
        // 方法签名(方法的描述)
        NSMethodSignature *signature = [[self class] instanceMethodSignatureForSelector:selector];
        if (signature == nil) {
            
            //可以抛出异常也可以不操作。
        }
        
        // NSInvocation : 利用一个NSInvocation对象包装一次方法调用(方法调用者、方法名、方法参数、方法返回值)
        NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:signature];
        invocation.target = self;
        invocation.selector = selector;
        
        // 设置参数
        NSInteger paramsCount = signature.numberOfArguments - 2; // 除self、_cmd以外的参数个数
        paramsCount = MIN(paramsCount, objects.count);
        for (NSInteger i = 0; i < paramsCount; i++) {
            id object = objects[i];
            if ([object isKindOfClass:[NSNull class]]) continue;
            [invocation setArgument:&object atIndex:i + 2];
        }
        
        // 调用方法
        [invocation invoke];
        
        // 获取返回值
        id returnValue = nil;
        if (signature.methodReturnLength) { // 有返回值类型,才去获得返回值
            [invocation getReturnValue:&returnValue];
        }
        
        return returnValue;
    }
    - (void)test {
        NSString *str = @"字符串";
        NSNumber *num = @20;
        NSArray *arr = @[@"数组值1", @"数组值2"];
        SEL sel = NSSelectorFromString(@"NSInvocationWithString:withNum:withArray:");
        NSArray *objs = [NSArray arrayWithObjects:str, num, arr, nil];
        
        [self performSelector:sel withObjects:objs];
    }
    
    - (void)NSInvocationWithString:(NSString *)string withNum:(NSNumber *)number withArray:(NSArray *)array {
        NSLog(@"%@, %@, %@", string, number, array[0]);
    }
    

    第二种:省略...

    第三种:objc_msgSend

    - (void)test {
        NSString *str = @"字符串objc_msgSend";
        NSNumber *num = @20;
        NSArray *arr = @[@"数组值1", @"数组值2"];
        SEL sel = NSSelectorFromString(@"ObjcMsgSendWithString:withNum:withArray:");
        
        ((void (*) (id, SEL, NSString *, NSNumber *, NSArray *)) objc_msgSend) (self, sel, str, num, arr);
    }
    - (void)ObjcMsgSendWithString:(NSString *)string withNum:(NSNumber *)number withArray:(NSArray *)array {
        NSLog(@"%@, %@, %@", string, number, array[0]);
    }
    

    如果参数中有结构体怎么办?

    可以把结构体转换成对象。

    typedef struct ParameterStruct{
        int a;
        int b;
    }MyStruct;
    
    - (void)test {
        
        NSString *str = @"字符串 把结构体转换为对象";
        NSNumber *num = @20;
        NSArray *arr = @[@"数组值1", @"数组值2"];
        
        MyStruct mystruct = {10,20};
        NSValue *value = [NSValue valueWithBytes:&mystruct objCType:@encode(MyStruct)];
        
        SEL sel = NSSelectorFromString(@"NSInvocationWithString:withNum:withArray:withValue:");
        NSArray *objs = [NSArray arrayWithObjects:str, num, arr, value,nil];
        
        [self performSelector:sel withObjects:objs]; // 同第一种方法
    }
    
    - (void)NSInvocationWithString:(NSString *)string withNum:(NSNumber *)number withArray:(NSArray *)array withValue:(NSValue *)value{
        
        MyStruct struceBack;
        [value getValue:&struceBack];
        
        NSLog(@"%@, %@, %@, %d", string, number, array[0],struceBack.a);
    }
    

    转自公众号:NA分享

    相关文章

      网友评论

          本文标题:【iOS】performSelector详解(上篇)

          本文链接:https://www.haomeiwen.com/subject/plxcxctx.html