3

How can i call one method from a NSThread?

i can call one method like below:

[regionsMapView addAnnotation:addAnnotation];

How can i call that in a NSThread? I have used like below:

[NSThread detachNewThreadSelector:@selector(addAnnotation) 
                         toTarget:regionsMapView 
                       withObject:addAnnotation];

But application crashes with log like below:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSThread initWithTarget:selector:object:]: target does not implement selector (*** -[MKMapView addAnnotation])'
*** Call stack at first throw:
(
    0   CoreFoundation                      0x0182b5a9 __exceptionPreprocess + 185
    1   libobjc.A.dylib                     0x0197f313 objc_exception_throw + 44
    2   CoreFoundation                      0x017e3ef8 +[NSException raise:format:arguments:] + 136
    3   CoreFoundation                      0x017e3e6a +[NSException raise:format:] + 58
    4   Foundation                          0x0006c392 -[NSThread initWithTarget:selector:object:] + 146
    5   Foundation                          0x0006c2d3 +[NSThread detachNewThreadSelector:toTarget:withObject:] + 98
    6   MJA                                 0x00008a8e -[TaetPaViewController showAddress:withItem:] + 389
    7   MJA                                 0x00008eb9 -[TaetPaViewController UpdateInfo] + 784
    8   MJA                                 0x0000835a -[TaetPaViewController parseTheLocalXml] + 408
    9   Foundation                          0x0006ccf4 -[NSThread main] + 81
    10  Foundation                          0x0006cc80 __NSThread__main__ + 1387
    11  libSystem.B.dylib                   0x911a47fd _pthread_start + 345
    12  libSystem.B.dylib                   0x911a4682 thread_start + 34
)
terminate called after throwing an instance of 'NSException'
Program received signal:  “SIGABRT”.
itsazzad
  • 6,868
  • 7
  • 69
  • 89

1 Answers1

3

You forgot the colon at the end.

@selector(addAnnotation)

is not

@selector(addAnnotation:)

The first calls a method that takes no parameters and the second takes one parameter. Your code should be this

[NSThread detachNewThreadSelector:@selector(addAnnotation:) 
                     toTarget:regionsMapView 
                   withObject:addAnnotation];
Pierre
  • 2,019
  • 14
  • 12