performSelector
NSObject.h
- (id)performSelector:(SEL)aSelector;
- (id)performSelector:(SEL)aSelector withObject:(id)object;
- (id)performSelector:(SEL)aSelector withObject:(id)object1 withObject:(id)object2;
performSelector
的使用
1.无参数调用
[self sendMessage];
- (void)sendMessageWithNumber:(NSString *)number {
NSLog(@"%@",number);
}
2.单个参数调用
[self sendMessageWithNumber:@"Number"];
- (void)sendMessageWithNumber:(NSString *)number {
NSLog(@"%@",number);
}
3.两个参数调用
[self sendMessageWithNumber:@"Number" count:@"count"];
- (id)sendMessageWithNumber:(NSString *)number
count:(NSString *)count {
NSLog(@"%@ %@",number,count);
return nil;
}
performSelector
不能传递三个以及三个以上的参数。假如需要传的话,可以使用 NSInvocation
或者是 objc_msgSend
performSelectorOnMainThread
NSThread.h
- (void)performSelectorOnMainThread:(SEL)aSelector withObject:(nullable id)arg waitUntilDone:(BOOL)wait;
基于默认模式调用主线程中接收器的方法,该方法不应该有明确的返回值并且最多只有一个id类型的参数,或者没有参数。wait
指定是否阻塞当前线程直到指定选择器在主线程中执行完毕。选择YES会阻塞这个线程;选择NO,本方法会立刻返回。如果当前线程也是主线程,选择YES,消息会立即派发,处理。
``
例如
NSLog(@"1");
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"2");
sleep(2);
NSLog(@"3");
});
[self performSelectorOnMainThread:@selector(sendMessageWithNumber:) withObject:@"4" waitUntilDone:YES];
- (void)sendMessageWithNumber:(NSString *)number {
NSLog(@"%@",number);
}
输出结果
1 4 2 3
- (void)performSelector:(SEL)aSelector onThread:(NSThread *)thrwithObject:(nullable id)arg waitUntilDone:(BOOL)wait;
调用指定线程中的某个方法。
- (void)performSelector:(SEL)aSelector onThread:(NSThread *)thrwithObject:(nullable id)arg waitUntilDone:(BOOL)wait;
开启子线程在后台运行
performSelector afterDelay
NSRunLoop.h
- (void)performSelector:(SEL)aSelector withObject:(id)anArgument afterDelay:(NSTimeInterval)delay inModes:(NSArray *)modes;
- (void)performSelector:(SEL)aSelector withObject:(id)anArgument afterDelay:(NSTimeInterval)delay;
这两个方法为异步执行,即使delay传参为0,仍为异步执行。只能在主线程中执行,在子线程中不会调到aSelector方法。可用于当点击UI中一个按钮会触发一个消耗系统性能的事件,在事件执行期间按钮会一直处于高亮状态,此时可以调用该方法去异步的处理该事件,就能避免上面的问题。
在方法未到执行时间之前,取消方法为:
+ (void)cancelPreviousPerformRequestsWithTarget:(id)aTarget selector:(SEL)aSelector object:(id)anArgument;
+ (void)cancelPreviousPerformRequestsWithTarget:(id)aTarget;
注意:调用该方法之前或在该方法所在的viewController生命周期结束的时候去调用取消函数,以确保不会引起内存泄露。
例如
NSLog(@"1");
[self performSelector:@selector(sendMessageWithNumber:) withObject:@"2" afterDelay:3];
NSLog(@"3");
打印出 1 3 2
而如下则打印出 1 3
dispatch_async(dispatch_get_global_queue(0, DISPATCH_QUEUE_PRIORITY_DEFAULT), ^{
NSLog(@"1");
[self performSelector:@selector(sendMessageWithNumber:) withObject:@"2" afterDelay:3];
NSLog(@"3");
});
NSLog(@"1");
[self performSelector:@selector(sendMessageWithNumber:) withObject:@"2" afterDelay:3];
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(sendMessageWithNumber:) object:@"2"];
NSLog(@"3");
打印结果 1 3
,因为打印2
的事件已经在执行前被取消了
NSInvocation
SEL selector = @selector(sendMessageWithNumber:count:);
// 方法签名
NSMethodSignature *singnature = [self methodSignatureForSelector:selector];
if (singnature) {
// NSInvocation : 利用一个NSInvocation对象包装一次方法调用(方法调用者、方法名、方法参数、方法返回值)
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:singnature];
[invocation setTarget:self];
[invocation setSelector:@selector(sendMessageWithNumber:count:)];
NSString *number = @"1111";
NSString *number2 = @"2ss";
// 设置参数
[invocation setArgument:&number atIndex:2];
[invocation setArgument:&number2 atIndex:3];
// 调用方法
[invocation invoke];
// 获取返回值
NSString *result;
if (singnature.methodReturnLength != 0) {// 有返回值类型,才去获得返回值
[invocation getReturnValue:&result];
}
NSLog(@"%@",result);
} else {
//可以抛出异常也可以不操作。
NSLog(@"singnature is nil");
}
调用时可以传入多个参数;
但是要注意判断是否是签名成功;
因为前两个参数分别是方法调用者、方法名,因为自己传入的参数是从第三个开始;
objc_msgSend
objc_msgSend
SEL selector = @selector(sendMessageWithNumber:count:);
NSString *result = ((NSString * (*) (id, SEL, NSString *, NSString *))objc_msgSend)(self, selector,@"2",@"3");
NSLog(@"%@",result);
打印结果如下
2 3
3333