不常用的基本方法

本文详细介绍了Objective-C中使用performSelector方法的不同方式,包括无参数、单参数及双参数的方法调用,并解释了如何实现方法的延迟调用及在不同线程上的执行。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

//1.使用perform调用不带参数的方法
//SEL是一个类型:方法选择器  @selector(方法名)
[self performSelector:@selector(demo1)];
     
//2.使用perform调用带有一个参数的方法
[self performSelector:@selector(demo2:) withObject:@"这是一个参数"];
     
//3.使用perform调用带有两个个参数的方法
[self performSelector:@selector(demo3:withName:) withObject:@"参数1" withObject:@"参数2"];
    
//4.延迟调用某一个方法 异步
//代码不会停留在这里,这个任务会挂载后台,后面代码继续执行

[self performSelector:@selector(demo1) withObject:NULL afterDelay:2.5];



线程相关:  
//6,到主线程上去执行一个方法,参数,是否等待完成  
- (void)performSelectorOnMainThread:(SEL)aSelector withObject:(id)arg waitUntilDone:(BOOL)wait;  
//7,和6一样  
- (void)performSelectorOnMainThread:(SEL)aSelector withObject:(id)arg waitUntilDone:(BOOL)wait modes:(NSArray *)array;  
       
//8,在指定的线程上去执行一个方法  
- (void)performSelector:(SEL)aSelector onThread:(NSThread *)thr withObject:(id)arg waitUntilDone:(BOOL)wait modes:(NSArray *)array NS_AVAILABLE(10_5, 2_0);  
//9,在指定的线程上执行一个方法  
- (void)performSelector:(SEL)aSelector onThread:(NSThread *)thr withObject:(id)arg waitUntilDone:(BOOL)wait NS_AVAILABLE(10_5, 2_0);  
// equivalent to the first method with kCFRunLoopCommonModes  
//10,开辟新的线程执行一个方法  
- (void)performSelectorInBackground:(SEL)aSelector withObject:(id)arg NS_AVAILABLE(10_5, 2_0);  
// waitUntilDone:表示是否等待@selector(setImage:)方法执行完成




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值