calling selector with two arguments on NSThread issue

okami picture okami · Feb 24, 2010 · Viewed 8.2k times · Source

I'd like to make a Thread with multiple arguments. Is it possible? I have the function:

-(void) loginWithUser:(NSString *) user password:(NSString *) password {
}

And I want to call this function as a selector:


[NSThread detachNewThreadSelector:@selector(loginWithUser:user:password:) toTarget:self withObject:@"someusername" withObject:@"somepassword"]; // this is wrong


How to pass two arguments on withObject parameter on this detachNewThreadSelect function?

Is it possible?

Answer

ennuikiller picture ennuikiller · Feb 24, 2010

You need to pass the extra parameters in an object passed to withObject like so:

NSDictionary *extraParams = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:@"user",@"password",nil] andKeys:[NSArray arrayWithObjects:@"valueForUser",@"valueForPassword",nil]]

[NSThread detachNewThreadSelector:@selector(loginWithUser:) toTarget:self withObject:extraParams];