performSelector浅析

来源:互联网 发布:企业办公网络组网方案 编辑:程序博客网 时间:2024/06/01 20:46
- (void)viewDidLoad{    [super viewDidLoad];// Do any additional setup after loading the view, typically from a nib.       [self performSelector:@selector(printf) withObject:nil afterDelay:0];        [self test];}-(void)test{    for (int i = 10 ; i < 20; i++) {        NSLog(@"%d",i);            }    NSLog(@"thread is %f",[[NSThread currentThread]threadPriority]);}-(void)printf{    for (int i = 0 ; i < 10; i++) {        NSLog(@"%d",i);            }   NSLog(@"thread is %f",[[NSThread currentThread]threadPriority]);}


- (void)viewDidLoad{    [super viewDidLoad];// Do any additional setup after loading the view, typically from a nib.       [self performSelector:@selector(printf) withObject:nil afterDelay:0];    for (int i = 10 ; i < 20; i++) {        NSLog(@"%d",i);            }    NSLog(@"thread is %f",[[NSThread currentThread]threadPriority]);}-(void)printf{    for (int i = 0 ; i < 10; i++) {        NSLog(@"%d",i);            }   NSLog(@"thread is %f",[[NSThread currentThread]threadPriority]);}

2014-08-03 22:14:12.704 HelloWorld[4984:60b] 102014-08-03 22:14:12.707 HelloWorld[4984:60b] 112014-08-03 22:14:12.708 HelloWorld[4984:60b] 122014-08-03 22:14:12.710 HelloWorld[4984:60b] 132014-08-03 22:14:12.712 HelloWorld[4984:60b] 142014-08-03 22:14:12.713 HelloWorld[4984:60b] 152014-08-03 22:14:12.714 HelloWorld[4984:60b] 162014-08-03 22:14:12.715 HelloWorld[4984:60b] 172014-08-03 22:14:12.716 HelloWorld[4984:60b] 182014-08-03 22:14:12.717 HelloWorld[4984:60b] 192014-08-03 22:14:12.719 HelloWorld[4984:60b] thread is 0.7580652014-08-03 22:14:12.732 HelloWorld[4984:60b] 02014-08-03 22:14:12.734 HelloWorld[4984:60b] 12014-08-03 22:14:12.735 HelloWorld[4984:60b] 22014-08-03 22:14:12.736 HelloWorld[4984:60b] 32014-08-03 22:14:12.737 HelloWorld[4984:60b] 42014-08-03 22:14:12.738 HelloWorld[4984:60b] 52014-08-03 22:14:12.739 HelloWorld[4984:60b] 62014-08-03 22:14:12.740 HelloWorld[4984:60b] 72014-08-03 22:14:12.742 HelloWorld[4984:60b] 82014-08-03 22:14:12.746 HelloWorld[4984:60b] 92014-08-03 22:14:12.747 HelloWorld[4984:60b] thread is 0.758065


以上得知:都在主线程中执行,但是

performSelector
优先级比[self test]直接调用的优先级低。要等到所在的函数中其他的任务都执行完了,才执行。

2.performSelector是运行时系统负责去找函数/方法的,在编译时候不做任何校验;但是直接调用肯定在编译是会校验。如果test2不存在,那么直接调用 在编译时候就能够发现(借助Xcode可以写完就发现),但是使用performSelector的话一定是在运行时候才能发现(此时程序崩溃)


线程在运行过程中,可能需要与其它线程进行通信。Cocoa为iOS线程间通信提供2种方式,performSelector和Port。

(1) performSelector方式

在应用程序主线程中做事情:
performSelectorOnMainThread:withObject:waitUntilDone: performSelectorOnMainThread:withObject:waitUntilDone:modes:

在指定线程中做事情:
performSelector:onThread:withObject:waitUntilDone: performSelector:onThread:withObject:waitUntilDone:modes:

在当前线程中做事情:
performSelector:withObject:afterDelay: performSelector:withObject:afterDelay:inModes:

取消发送给当前线程的某个消息:
cancelPreviousPerformRequestsWithTarget: cancelPreviousPerformRequestsWithTarget:selector:object:

如在我们在某个线程中下载数据,下载完成之后要通知主线程中更新界面等等,可以使用如下接口:

- (void)myThreadMainMethod
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
// to do something in your thread job
...
[self performSelectorOnMainThread:@selector(updateUI) withObject:nil waitUntilDone:NO]; [pool release];
}


->>>>>A动态调用B的私有方法(B的头文件中没有声明)

B.m

@implementation ViewController- (void)viewDidLoad{    [super viewDidLoad];// Do any additional setup after loading the view, typically from a nib.   NSString * str = @"hello/";    NSLog(@"%@",[str pathExtension]);}-(void)printfhi{    NSLog(@"hihihihihi")    ;}

A.m

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{    // Override point for customization after application launch.    UIStoryboard * storBoard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];    ViewController * vc = [storBoard instantiateViewControllerWithIdentifier:@"ViewController"];    [vc performSelector:@selector(printfhi) withObject:nil];    self.window.rootViewController = vc;    return YES;}
输出结果:
2014-08-07 21:41:57.690 HelloWorld[2245:60b] hihihihihi


0 0