performSelector浅析

- (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] 10
2014-08-03 22:14:12.707 HelloWorld[4984:60b] 11
2014-08-03 22:14:12.708 HelloWorld[4984:60b] 12
2014-08-03 22:14:12.710 HelloWorld[4984:60b] 13
2014-08-03 22:14:12.712 HelloWorld[4984:60b] 14
2014-08-03 22:14:12.713 HelloWorld[4984:60b] 15
2014-08-03 22:14:12.714 HelloWorld[4984:60b] 16
2014-08-03 22:14:12.715 HelloWorld[4984:60b] 17
2014-08-03 22:14:12.716 HelloWorld[4984:60b] 18
2014-08-03 22:14:12.717 HelloWorld[4984:60b] 19
2014-08-03 22:14:12.719 HelloWorld[4984:60b] thread is 0.758065
2014-08-03 22:14:12.732 HelloWorld[4984:60b] 0
2014-08-03 22:14:12.734 HelloWorld[4984:60b] 1
2014-08-03 22:14:12.735 HelloWorld[4984:60b] 2
2014-08-03 22:14:12.736 HelloWorld[4984:60b] 3
2014-08-03 22:14:12.737 HelloWorld[4984:60b] 4
2014-08-03 22:14:12.738 HelloWorld[4984:60b] 5
2014-08-03 22:14:12.739 HelloWorld[4984:60b] 6
2014-08-03 22:14:12.740 HelloWorld[4984:60b] 7
2014-08-03 22:14:12.742 HelloWorld[4984:60b] 8
2014-08-03 22:14:12.746 HelloWorld[4984:60b] 9
2014-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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
- (void)close { // Empty queues. [self emptyQueues]; [partialReadBuffer release]; partialReadBuffer = nil; [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(disconnect) object:nil]; // Close streams. if (theReadStream != NULL) { CFReadStreamSetClient(theReadStream, kCFStreamEventNone, NULL, NULL); CFReadStreamUnscheduleFromRunLoop (theReadStream, theRunLoop, kCFRunLoopDefaultMode); CFReadStreamClose (theReadStream); CFRelease (theReadStream); theReadStream = NULL; } if (theWriteStream != NULL) { CFWriteStreamSetClient(theWriteStream, kCFStreamEventNone, NULL, NULL); CFWriteStreamUnscheduleFromRunLoop (theWriteStream, theRunLoop, kCFRunLoopDefaultMode); CFWriteStreamClose (theWriteStream); CFRelease (theWriteStream); theWriteStream = NULL; } // Close sockets. if (theSocket != NULL) { CFSocketInvalidate (theSocket); CFRelease (theSocket); theSocket = NULL; } if (theSocket6 != NULL) { CFSocketInvalidate (theSocket6); CFRelease (theSocket6); theSocket6 = NULL; } if (theSource != NULL) { CFRunLoopRemoveSource (theRunLoop, theSource, kCFRunLoopDefaultMode); CFRelease (theSource); theSource = NULL; } if (theSource6 != NULL) { CFRunLoopRemoveSource (theRunLoop, theSource6, kCFRunLoopDefaultMode); CFRelease (theSource6); theSource6 = NULL; } theRunLoop = NULL; // If the client has passed the connect/accept method, then the connection has at least begun. // Notify delegate that it is now ending. if (theFlags & kDidPassConnectMethod) { // Delay notification to give him freedom to release without returning here and core-dumping. if ([theDelegate respondsToSelector: @selector(onSocketDidDisconnect:)]) { //[theDelegate performSelector:@selector(onSocketDidDisconnect:) withObject:self afterDelay:0]; [theDelegate onSocketDidDisconnect:self]; } } // Clear flags. theFlags = 0x00; }
06-13
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值