【问题标题】:Call one method with more than one parameter from a NSThread从 NSThread 调用具有多个参数的一种方法
【发布时间】:2011-07-04 09:38:57
【问题描述】:

如何从 NSThread 调用一种方法?

我可以调用如下的一种方法:

[regionsMapView addAnnotation:addAnnotation];

如何在 NSThread 中调用它? 我用过如下:

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

但应用程序崩溃并显示如下日志:

*** 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”.

【问题讨论】:

    标签: objective-c ios xcode nsthread


    【解决方案1】:

    你忘记了最后的冒号。

    @selector(addAnnotation)
    

    不是

    @selector(addAnnotation:)
    

    第一个调用不带参数的方法,第二个调用一个参数。 你的代码应该是这样的

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

    【讨论】:

    • 感谢您的回答。如果方法有多个参数会怎样?
    • 应用程序将崩溃。只能使用带一个参数且没有返回值的方法。
    猜你喜欢
    • 2012-05-19
    • 2011-12-10
    • 2011-10-14
    • 2019-10-22
    • 1970-01-01
    • 2017-09-06
    • 1970-01-01
    • 1970-01-01
    • 2015-08-14
    相关资源
    最近更新 更多