uncaught exception 'NSInvalidArgumentException' ->Objective C
I was trying out a sample thread program from google and i am getting a runtime exception.
Is there any website that gives an example of how to use runloops along with threads. I need to set two events and spawn a thread and to do another function parallely.
// Runner.m
#import "Runner.h"
@implementation Runner
- (void)rumMe:(id)ignored {
NSLog(@"Running with threads!!");
}
@end
// Runner.h
@interface Runner : NSObject
-(void)rumMe:(id)ignored;
@end
// Thread1.m
#import <Foundation/Foundation.h>
#import "Runner.h"
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[开发者_开发知识库NSAutoreleasePool alloc] init];
Runner* runner = [Runner new];
[NSThread detachNewThreadSelector:@selector(runMe:) toTarget:runner withObject:nil];
[pool drain];
return 0;
}
Runtime Exception:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '***
-[NSThread initWithTarget:selector:object:]: target does not implement selector (***
-[Runner runMe:])'
First part: you had a typo
// method declaration
rumMe: with an _m_
// call
runMe: with an _n_
Second part: your main function is returning and causing the program to exit before you have given the thread a chance to do anything. In this simple simple example, you could simply
sleep(2);
right after the call to detachNewThreadSelector:
In more complex cases, you might need to make a call to CFRunLoopRun();
on the main thread, or take other action to keep the second thread alive.
You have made a typo. The method in Runner
is defined as rumMe
, but in the main program you use runMe
.
精彩评论