如何将 NSTimeInterval 对象传递给 detachNewThreadSelector withObject 参数?
我对 Objective C 语法仍然很陌生,所以我可能会让这个过于复杂,但我似乎无法弄清楚如何将 NSTimeInterval 传递给线程。
我想启动一个休眠 x 秒的线程,从主线程发送参数如下:
[NSThread detachNewThreadSelector:@selector(StartServerSynchThread) toTarget:self withObject:5];
- (void) StartServerSynchThread:(NSTimeInterval *)sleepSecondsInterval {
[NSThread sleepForTimeInterval:sleepSecondsInterval];
}
但是编译器一直给我一个语法错误。我不确定具体应该如何做。任何帮助将不胜感激。谢谢!
I'm still new to Objective C syntax, so I might be overcomplicating this, but I can't seem to figure out how to pass an NSTimeInterval to a thread.
I want to initiate a thread that sleeps for x seconds parameter sent from main thread as follows:
[NSThread detachNewThreadSelector:@selector(StartServerSynchThread) toTarget:self withObject:5];
- (void) StartServerSynchThread:(NSTimeInterval *)sleepSecondsInterval {
[NSThread sleepForTimeInterval:sleepSecondsInterval];
}
But the compiler keeps giving me a syntax error. I'm not sure exactly how it should be done. Any help would be appreciated. Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这与 @Georg 的答案几乎完全相同,但使用了正确的类型。 :)
如果将 NSTimeInterval 装箱到
NSNumber
(NSValue
的子类)中,则可以将其传入:This is almost exactly the same as @Georg's answer, but using the proper types. :)
If you box the NSTimeInterval into an
NSNumber
(a subclass ofNSValue
), you can pass that in instead:object
参数为id
类型,这意味着只能传递类类型的对象。整数,例如您尝试传递的5
以及NSTimeInterval
(实际上只是double
的typedef
) 是基本类型,而不是类类型。您可以使用
NSNumber
作为包装器或传递NSDate
。The
object
parameter hasid
type, which means only class-type objects can be passed. Integers, like the5
you are trying to pass, as well asNSTimeInterval
(actually just atypedef
fordouble
), are fundamental types, not class-types.You can use
NSNumber
as a wrapper or pass aNSDate
instead.