2011-04-05 77 views
0

我從來沒有使用過NSThread,我想知道是否有可能將參數傳遞給它,如果是這樣,如何?例如:如何通過NSThread傳遞參數

NSObject *phrase = @"I JUST MADE IT THROUGH TO THE THREAD METHOD!"; 

[NSThread detachNewThreadSelector:@selector (run_thread) 
         toTarget:self 
         withObject:phrase]; 

然後


-(void)run_thread 
{ 
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; 

NSLog(@"RECORD FILE PATH ----> %@", phrase); 

[pool drain]; 

} 

我想你明白我要怎樣做。有什麼建議?

回答

6

就快:

NSObject *phrase = @"I JUST MADE IT THROUGH TO THE THREAD METHOD!"; 

[NSThread detachNewThreadSelector:@selector (run_thread:) // have to add colon 
        toTarget:self 
        withObject:phrase]; 

-(void)run_thread:(NSObject*)phrase // change method signature to support taking an NSObject 
{ 
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; 

    NSLog(@"RECORD FILE PATH ----> %@", phrase); 

    [pool drain]; 

} 
1
[NSThread detachNewThreadSelector:@selector(run_thread:) 
         toTarget:self 
         withObject:phrase]; 

-(void)run_thread:(NSString *)phrase 
{ 
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; 

    NSLog(@"RECORD FILE PATH ----> %@", phrase); 

    [pool drain]; 
}