0
- (void)TargetHit:(int)target{

    void (^threadBlock)(void) = ^{
        NSLog(@"respond to selector %d", [self respondsToSelector:@selector(changeImageOfTarget:)]);

        [[NSOperationQueue mainQueue] performSelectorOnMainThread:@selector(changeImageOfTarget:) withObject:[NSNumber numberWithInt:target] waitUntilDone:YES];
        [NSThread sleepForTimeInterval:1.0]; 
        [[NSOperationQueue mainQueue] performSelectorOnMainThread:@selector(changeImageOfTarget:) withObject:[NSNumber numberWithInt:target] waitUntilDone:YES];
    };

    dispatch_async(dispatch_queue_create(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), threadBlock);
}

I can understand why my code returns YES to responds to selector but when I try and perform it I get an error message and the app crashes.

[NSOperationQueue changeImageOfTarget:]: unrecognized selector sent to instance 0x6a39810'

It's also the same for

 [[NSThread currentThread] performSelectorOnMainThread:@selector(changeImageOfTarget:) withObject:[NSNumber numberWithInt:target] waitUntilDone:YES];

How am I able to perform a method or @selector on the main thread?

greeness
  • 15,956
  • 5
  • 50
  • 80
geminiCoder
  • 2,918
  • 2
  • 29
  • 50

1 Answers1

4

selfmight responde to the selector. The problem is that [NSOperationQueue mainQueue]doesn't.

How am I able to perform a method or @selector on the main thread?

Just call it on the object where you want the action to be performed:

[self performSelectorOnMainThread:@selector(changeImageOfTarget:) withObject:[NSNumber numberWithInt:target] waitUntilDone:YES];
Rui Peres
  • 25,741
  • 9
  • 87
  • 137