iOS底层系列32 -- performSelector方法的探

2022-04-02  本文已影响0人  YanZi_33

performSelector方法

延迟执行 -- 与RunLoop有关

- (void)viewDidLoad {
    [super viewDidLoad];
    [self performSelector:@selector(test) withObject:nil afterDelay:3];
}

- (void)test {
    NSLog(@"%s",__func__);
    NSLog(@"%@",[NSThread currentThread]);
}
@end
@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
   
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        [self performSelector:@selector(test) withObject:nil afterDelay:3];
    });
}

- (void)test {
    NSLog(@"%s",__func__);
    NSLog(@"%@",[NSThread currentThread]);
}
@end
@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
 
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        [self performSelector:@selector(test) withObject:nil afterDelay:3];
        [[NSRunLoop currentRunLoop] run];
    });
}

- (void)test {
    NSLog(@"%s",__func__);
    NSLog(@"%@",[NSThread currentThread]);
}
@end

开启子线程执行任务 -- 与多线程有关

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        NSLog(@"%@",[NSThread currentThread]);
        NSLog(@"11111");
        [self performSelector:@selector(test) onThread:[NSThread currentThread] withObject:nil waitUntilDone:YES];
        NSLog(@"22222");
    });
}

- (void)test {
    NSLog(@"%s",__func__);
    NSLog(@"%@",[NSThread currentThread]);
}
@end
image.png

performSelector:方法传递多参数的实现方案

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    NSDictionary *params = @{
        @"name":@"yanzi",
        @"age":@"30"
    };
    [self performSelector:@selector(test:) withObject:params];

}

- (void)test:(NSDictionary *)params {
    NSLog(@"%@--%@",params[@"name"],params[@"age"]);
}
@end
#import "ViewController.h"
#import <objc/message.h>

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    ((void (*)(id,SEL,NSString *, NSString *, NSString *))objc_msgSend)(self, @selector(testWithParam:param2:param3:),@"111",@"222",@"333");
}

//有三个参数的方法
- (void)testWithParam:(NSString *)param1 param2:(NSString *)param2 param3:(NSString *)param3 {
    NSLog(@"param1:%@, param2:%@, param3:%@",param1, param2, param3);
}
@end
@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //调用方法
    NSArray *paramArray = @[@"112",@[@2,@13],@12];
    [self performSelector:@selector(testFunctionWithParam:param2:param3:) withObjects:paramArray];
}

//可以传多个参数的方法
- (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)testFunctionWithParam:(NSString *)param1 param2:(NSArray *)param2 param3:(NSInteger)param3 {
    NSLog(@"param1:%@, param2:%@, param3:%ld",param1, param2, param3);
}
@end
上一篇下一篇

猜你喜欢

热点阅读