Mla*_*den 1 multithreading objective-c
我的应用程序崩溃了
[NSThread detachNewThreadSelector: @selector(getJSON:) toTarget:self withObject:nil];
以下是getJSON函数的外观:
- (void)getJSON: (NSDate *)startTime endTime:(NSDate *)endTime;
这有什么问题?
虽然你们是正确的,但方法选择器是错误的,你的解决方案将无济于事,因为detachNewThreadSelector的选择器必须只有一个参数.
withObject参数将作为唯一参数传递给您的线程方法.
如果您的线程方法想要接收开始和结束时间,那么通常的方法是使用NSDictionary,例如:
[NSThread detachNewThreadSelector:@selector(getJSON:)
toTarget:self
withObject:[NSDictionary dictionaryWithObjectsAndKeys:
startTime, @"startTime",
endTime, @"endTime",
nil]];
Run Code Online (Sandbox Code Playgroud)
那么线程方法就是
- (void) getJSON: (NSDictionary*) parameters
{
NSDate* startTime = [parameters objectForKey:@"startTime"];
NSDate* endTime = [parameters objectForKey:@"endTime"];
...
}
Run Code Online (Sandbox Code Playgroud)