[英]NSInvalidArgumentException when calling selector with arguments
我正在尝试使用带有一个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)编程的新手,所以对编码约定和最佳实践的任何评论将不胜感激(即使与我的问题无关,但与我的代码有关)
您的方法已声明为
-(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;
该方法:
-(void)displayMessageOnTextView:(NSString *)message
具有选择器displayMessageOnTextView:
参数本身的名称(在您的情况下为message
)不会显示在选择器中。 (这应该很直观:调用该方法时名称不一定会出现,因此它不会出现在选择器中。)因此,您需要使用@selector(displayMessageOnTextView:)
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.