NSInvalidArgumentException when calling selector with arguments
I'm trying to execute a method using selector with one NSString as parameter and fails. I have seen many questions (and answers) like this and tried most solutions but had no success. Please see code and errors below:
My MainView.m
is my view controller and it has a method:
-(void)displayMessageOnTextView:(NSString *)message
{
[tv1 setText:message];
}
And in MainView.h
:
@interface MainView : UIViewController {
UITextView *tv1;
}
-(void)displayMessageOnTextView:(NSString *)message;
....
I have another thread which is fired by MainView (this is from MainView.m
):
- (void) runTest
{
MyThread *t = [[MyThread alloc] initWithInt:13253];
[t setMainView:self];
[t run];
[t release];
}
and 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
Error:
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] *** Te开发者_如何学编程rminating 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'
NOTE:
I'm new to Objective-C (and generally iOS) programming, so any comment regarding coding conventions and best practice will be appreciated (even if not related to my question, but related to my code)Your method has been declared as
-(void)displayMessageOnTextView:(NSString *)message;
This means that the method name is displayMessageOnTextView:
and the corresponding selector is @selector(displayMessageOnTextView:)
. Note that a method name does not include parameter (variable) names.
In your -run
method, change
[_view performSelectorOnMainThread:@selector(displayMessageOnTextView:message:)
withObject:s
waitUntilDone:YES];
to
[_view performSelectorOnMainThread:@selector(displayMessageOnTextView:)
withObject:s
waitUntilDone:YES];
and that runtime error shouldn’t happen again.
For the record, a method declaration corresponding to displayMessageOnTextView:message:
would be:
- (void)displayMessageOnTextView:(type1)parameter1 message:(type2)parameter2;
This method:
-(void)displayMessageOnTextView:(NSString *)message
has the selector displayMessageOnTextView:
. The name of the parameter itself (message
in your case) does not appear in the selector. (This should be sort of intuitive: the name doesn't necessarily appear when you call the method, so it doesn't appear in the selector.) So you need to be using @selector(displayMessageOnTextView:)
.
精彩评论