getting the object passed to the NSThread selector

前端 未结 2 706
南方客
南方客 2021-01-07 16:03

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 numb

2条回答
  •  借酒劲吻你
    2021-01-07 16:29

    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.

提交回复
热议问题