views:

932

answers:

3

Hi guys,

My application crashes on this line [NSThread detachNewThreadSelector: @selector(getJSON:) toTarget:self withObject:nil];

Here is how the getJSON funciton looks like: - (void)getJSON: (NSDate *)startTime endTime:(NSDate *)endTime;

What it the problem?

+1  A: 

It looks like you aren't specifying the method properly; Each argument is part of the method name:

[NSThread detachNewThreadSelector:@selector(getJSON:endTime:) 
                         toTarget:self 
                       withObject:nil];
Abizern
correct one . thx
Mladen
+2  A: 

The selector for - (void)getJSON:(NSDate *)startTime endTime:(NSDate *)endTime is

@selector(getJSON:endTime:)
monowerker
+6  A: 

While you guys are correct, the method selector is wrong, your solution will not help because the selector for detachNewThreadSelector must take only one argument.

The withObject parameter will be passed to your thread method as its one an only parameter.

If your threaded method wants to receive a start and end time, then the normal way to do this would be with an NSDictionary, something like:

[NSThread detachNewThreadSelector:@selector(getJSON:) 
                         toTarget:self 
                       withObject:[NSDictionary dictionaryWithObjectsAndKeys:
                             startTime, @"startTime",
                             endTime, @"endTime",
                             nil]];

Then the thread method would be

- (void) getJSON: (NSDictionary*) parameters
{
   NSDate* startTime = [parameters objectForKey:@"startTime"];
   NSDate* endTime = [parameters objectForKey:@"endTime"];
   ...
}
Peter N Lewis