2010-02-24 5 views
15

Je souhaite créer un thread avec plusieurs arguments. Est-ce possible? J'ai la fonction:Sélecteur d'appel avec deux arguments sur le problème NSThread

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

Et je veux appeler cette fonction comme un sélecteur:

 

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


Comment passer deux arguments sur le paramètre withObject sur cette fonction detachNewThreadSelect?

Est-ce possible?

Répondre

16

Vous devez passer les paramètres supplémentaires dans un objet passé à withObject comme ceci:

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

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

Merci pour une excellente réponse à cette question @Lance. – iPadDeveloper2011

+0

Vous avez oublié le point-virgule à la fin de votre première ligne ;-) – Ken

0

Enveloppez votre méthode sélectionnée avec une méthode d'emballage auxiliaire, « wrappingMethod », qui traite NSArray d'entrées en fonction de votre propre méthode avant d'appeler votre propre méthode au sein wrappingMethod. Maintenant détachez un NSThread qui sélectionne votre tout nouveau wrappingMethod et prend l'instance de NSArray pour withObject. A côté: Avoir un wrapper ici peut être particulièrement utile si votre méthode originale prend un ou plusieurs types primitifs en entrée et que vous devez ensuite travailler avec NSNumber ou NSStrings, disons, pour satisfaire NSThread.

6

C'est du haut de ma tête, non testé:

NSThread+ManyObjects.h:

@interface NSThread (ManyObjects) 

+ (void)detachNewThreadSelector:(SEL)aSelector 
         toTarget:(id)aTarget 
        withObject:(id)anArgument 
         andObject:(id)anotherArgument; 

@end 

NSThread+ManyObjects.m:

@implementation NSThread (ManyObjects) 

+ (void)detachNewThreadSelector:(SEL)aSelector 
         toTarget:(id)aTarget 
        withObject:(id)anArgument 
         andObject:(id)anotherArgument 
{ 
    NSMethodSignature *signature = [aTarget methodSignatureForSelector:aSelector]; 
    if (!signature) return; 

    NSInvocation* invocation = [NSInvocation invocationWithMethodSignature:signature]; 
    [invocation setTarget:aTarget]; 
    [invocation setSelector:aSelector]; 
    [invocation setArgument:&anArgument atIndex:2]; 
    [invocation setArgument:&anotherArgument atIndex:3]; 
    [invocation retainArguments]; 

    [self detachNewThreadSelector:@selector(invoke) toTarget:invocation withObject:nil]; 
} 

@end 

Et vous pouvez ensuite importer NSThread+ManyObjects et appeler

[NSThread detachNewThreadSelector:@selector(loginWithUser:user:password:) toTarget:self withObject:@"someusername" andObject:@"somepassword"]; 
0

Une mise à jour belle réponse ennuikiller:

NSDictionary* params = [NSDictionary dictionaryWithObjectsAndKeys:@"IMG_URL_VALUE",@"img_url",@"PARAM2_VALUE", @"param2", nil]; 

[NSThread detachNewThreadSelector:@selector(loadImage:) toTarget:self withObject:params];