【问题标题】:NSInvalidArgumentException when calling selector with arguments使用参数调用选择器时出现 NSInvalidArgumentException
【发布时间】:2011-04-19 23:18:20
【问题描述】:

我正在尝试使用一个 NSString 作为参数的选择器执行一种方法,但失败了。我见过很多这样的问题(和答案)并尝试了大多数解决方案但没有成功。请参阅下面的代码和错误:

我的MainView.m 是我的视图控制器,它有一个方法:

-(void)displayMessageOnTextView:(NSString *)message
{
    [tv1 setText:message];
}

MainView.h:

@interface MainView : UIViewController {
    UITextView *tv1;
}

-(void)displayMessageOnTextView:(NSString *)message;
....

我有另一个由 MainView 触发的线程(来自MainView.m):

- (void) runTest
{
    MyThread *t = [[MyThread alloc] initWithInt:13253];
    [t setMainView:self];
    [t run];
    [t release];
}

MyThread.m:

#import "MyThread.h"
#import "MainView.h"

@implementation MyThread

MainView *_view;
-(id)initWithInt:(int)someInt{
    _view = NULL;
    return self;
}

-(void)setMainView:(MainView*)view
{
    _view = view;
}

-(int)run{
    NSString *s = [NSString stringWithFormat:@"Display on Gui:%d", 1];
    [_view performSelectorOnMainThread:@selector(displayMessageOnTextView:message:) withObject:s waitUntilDone:YES];
}

@end

错误:

2011-04-20 02:08:11.553 myApp[22627:207] -[MainView displayMessageOnTextView:message:]: unrecognized selector sent to instance 0x4e383a0
2011-04-20 02:08:11.555 myApp[22627:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[MainView displayMessageOnTextView:message:]: unrecognized selector sent to instance 0x4e383a0'
*** Call stack at first throw:
(
    0   CoreFoundation                      0x00dc55a9 __exceptionPreprocess + 185
    1   libobjc.A.dylib                     0x00f19313 objc_exception_throw + 44
    2   CoreFoundation                      0x00dc70bb -[NSObject(NSObject) doesNotRecognizeSelector:] + 187
    3   CoreFoundation                      0x00d36966 ___forwarding___ + 966
    4   CoreFoundation                      0x00d36522 _CF_forwarding_prep_0 + 50
    5   Foundation                          0x0079e94e __NSThreadPerformPerform + 251
    6   CoreFoundation                      0x00da68ff __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 15
    7   CoreFoundation                      0x00d0488b __CFRunLoopDoSources0 + 571
    8   CoreFoundation                      0x00d03d86 __CFRunLoopRun + 470
    9   CoreFoundation                      0x00d03840 CFRunLoopRunSpecific + 208
    10  CoreFoundation                      0x00d03761 CFRunLoopRunInMode + 97
    11  GraphicsServices                    0x00ffd1c4 GSEventRunModal + 217
    12  GraphicsServices                    0x00ffd289 GSEventRun + 115
    13  UIKit                               0x00025c93 UIApplicationMain + 1160
    14  myApp                               0x000024c9 main + 121
    15  myApp                               0x00002445 start + 53
)
terminate called after throwing an instance of 'NSException'

注意:
我是 Objective-C(通常是 iOS)编程的新手,所以任何关于编码约定和最佳实践的评论都将不胜感激(即使与我的问题无关,但与我的代码相关)

【问题讨论】:

    标签: objective-c selector invalidargumentexception


    【解决方案1】:

    您的方法已声明为

    -(void)displayMessageOnTextView:(NSString *)message;
    

    这表示方法名是displayMessageOnTextView:,对应的选择器是@selector(displayMessageOnTextView:)。请注意,方法名称不包括参数(变量)名称。

    在您的 -run 方法中,更改

    [_view performSelectorOnMainThread:@selector(displayMessageOnTextView:message:)
                            withObject:s
                         waitUntilDone:YES];
    

    [_view performSelectorOnMainThread:@selector(displayMessageOnTextView:)
                            withObject:s
                         waitUntilDone:YES];
    

    并且该运行时错误不应再次发生。

    作为记录,对应于displayMessageOnTextView:message: 的方法声明将是:

    - (void)displayMessageOnTextView:(type1)parameter1 message:(type2)parameter2;
    

    【讨论】:

    • 嗯,首先,你是对的。它确实是这样工作的。其次,这是我的第一种方法,但由于某种原因它不起作用。非常感谢。
    【解决方案2】:

    这个方法:

    -(void)displayMessageOnTextView:(NSString *)message
    

    有选择器displayMessageOnTextView:。参数本身的名称(在您的情况下为message)不会出现在选择器中。 (这应该有点直观:名称不一定会在您调用方法时出现,因此它不会出现在选择器中。)所以您需要使用@selector(displayMessageOnTextView:)

    【讨论】:

    • 谢谢约翰。 (见对 Bavarious 的评论)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-31
    • 2011-09-09
    • 2013-10-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多