如何通过 NSThread 传递参数

发布于 2024-10-30 05:52:45 字数 514 浏览 1 评论 0原文

我以前从未使用过 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];

 }

我想你明白我正在尝试做什么。有什么建议吗?

I've never used NSThread before and I was wondering if it was possible to pass arguments into it and if so, how? For example:

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

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

then


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

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

[pool drain];

 }

I think you see what I'm trying to do. Any advice?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

み青杉依旧 2024-11-06 05:52:45

你就快到了:

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];

}

You're almost there:

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];

}
执妄 2024-11-06 05:52:45
[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];
}
[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];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文