将对象传递给 NSThread 选择器

发布于 2024-10-21 21:32:25 字数 1012 浏览 4 评论 0原文

当我创建 NSThread 时,我向它传递了一个我希望进程知道的数字。我可以理解如何设置数字,但我不知道如何从线程选择器方法中读取数字,以便我可以将其传递给计时器。

你怎么做?

-(void) setthread
{ 

// 在这里将数字传递给选择器

NSThread* timerThread = [[NSThread alloc] initWithTarget:self selector:@selector(startTimerThread) object:[NSNumber numberWithInt:index]];/
    [timerThread setThreadPriority:0.5];
    [timerThread start]; //start the thread 

}

// 不知道如何读取传递给该选择器的值

-(void) startTimerThread
{


    NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
    NSRunLoop* runLoop = [NSRunLoop currentRunLoop];
    [[NSTimer scheduledTimerWithTimeInterval: 0.1
                                      target: self
                                    selector: @selector(timerTick:)
                                    userInfo: thenumberhere
                                     repeats: YES] retain];

    [runLoop run];
    [pool release];
}

- (void)timerTick:(NSTimer *)timer
{
    //code
}

When I'm creating an NSThread I pass it a number that I want the process to be aware of. I can understand how to set the number but I cannot figure out how to read the number from the thread selector method so that I can then pass it to a timer.

How do you do it?

-(void) setthread
{ 

//passing the number to the selector here fine

NSThread* timerThread = [[NSThread alloc] initWithTarget:self selector:@selector(startTimerThread) object:[NSNumber numberWithInt:index]];/
    [timerThread setThreadPriority:0.5];
    [timerThread start]; //start the thread 

}

// don't get how to read the value passed to this selector

-(void) startTimerThread
{


    NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
    NSRunLoop* runLoop = [NSRunLoop currentRunLoop];
    [[NSTimer scheduledTimerWithTimeInterval: 0.1
                                      target: self
                                    selector: @selector(timerTick:)
                                    userInfo: thenumberhere
                                     repeats: YES] retain];

    [runLoop run];
    [pool release];
}

- (void)timerTick:(NSTimer *)timer
{
    //code
}

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

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

发布评论

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

评论(2

初相遇 2024-10-28 21:32:25

您指定的选择器是错误的:

@selector(startTimerThread)   // we are missing ':' at the end

它应该在末尾有 : ,如下所示:

@selector(startTimerThread:) 

这表明它是一个带有一个参数的选择器。

然后在 startTimerThread 方法中获取参数:

-(void) startTimerThread:(NSNumber *)myNumber {
    // ... etc

You're specifying your selector wrong:

@selector(startTimerThread)   // we are missing ':' at the end

It should have : at the end, like so:

@selector(startTimerThread:) 

This indicates it's a selector which takes one parameter.

Then take in the parameter in your startTimerThread method:

-(void) startTimerThread:(NSNumber *)myNumber {
    // ... etc
烟火散人牵绊 2024-10-28 21:32:25

那不行..
这将:

NSThread* timerThread = [[NSThread alloc] initWithTarget:self selector:@selector(startTimerThread:) object:[NSNumber numberWithInt:index]];


-(void) startTimerThread:(NSNumber *)thenumberhere
{


    NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
    NSRunLoop* runLoop = [NSRunLoop currentRunLoop];
    [[NSTimer scheduledTimerWithTimeInterval: 0.1
                                      target: self
                                    selector: @selector(timerTick:)
                                    userInfo: thenumberhere
                                     repeats: YES] retain];

    [runLoop run];
    [pool release];
}

您“忘记”将与选择器一起作为参数传递的对象添加到您实现的方法中。

that will not work..
this will:

NSThread* timerThread = [[NSThread alloc] initWithTarget:self selector:@selector(startTimerThread:) object:[NSNumber numberWithInt:index]];


-(void) startTimerThread:(NSNumber *)thenumberhere
{


    NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
    NSRunLoop* runLoop = [NSRunLoop currentRunLoop];
    [[NSTimer scheduledTimerWithTimeInterval: 0.1
                                      target: self
                                    selector: @selector(timerTick:)
                                    userInfo: thenumberhere
                                     repeats: YES] retain];

    [runLoop run];
    [pool release];
}

you 'forgot' to add the object, that you pass along with the selector as a parameter, to the method you implemented.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文