Reputation: 9
I was testing with a thread function. But the program always feedback an error. I guess that the error probably have something to do with the argument of the thread function, but i've no idea how to fix it. Thx in advance! The code is like:
-(void) doSomething:(id)p
{
if((int)p == 100)
NSLog(@"100");
else
NSLog(@"101");
}
int a = 100;
[self performSelectorInBackground:@selector(doSomething:) withObject:a];
Upvotes: 0
Views: 112
Reputation: 22726
you should do something like
[self performSelectorInBackground:@selector(doSomething:) withObject:[NSNumber numberWithInt:a]];
as withObject takes only object and int is primitive datatype.
Upvotes: 2
Reputation: 318894
Please read the docs for performSelectorInBackground:withObject:
. a
isn't an object, it's a primitive type.
You need to wrap the value in an NSNumber
.
-(void) doSomething:(NSNumber *)p
{
if([p intValue] == 100)
NSLog(@"100");
else
NSLog(@"101");
}
int a = 100;
[self performSelectorInBackground:@selector(doSomething:) withObject:@(a)];
Or you can use GCD:
-(void) doSomething:(int)p
{
if(p == 100)
NSLog(@"100");
else
NSLog(@"101");
}
int a = 100;
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
[self doSomething:a];
});
Upvotes: 3