2010-02-24 10 views
15

Mi piacerebbe creare una discussione con più argomenti. È possibile? Ho la funzione:selettore di chiamata con due argomenti su NSThread issue

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

E voglio chiamare questa funzione come selettore:

 

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


come passare due argomenti sul parametro withObject su questa funzione detachNewThreadSelect?

È possibile?

risposta

16

è necessario passare i parametri extra in un oggetto passato a withObject in questo modo:

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

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

Grazie per una grande risposta a questa domanda @Lance. – iPadDeveloper2011

+0

Hai dimenticato il punto e virgola alla fine della prima riga ;-) – Ken

0

avvolgere il metodo selezionato con un metodo di avvolgimento ausiliario, "wrappingMethod", che elabora un NSArray di ingressi per soddisfare la vostra proprio metodo prima di chiamare il proprio metodo entro wrappingMethod. Ora scollega un NSThread che seleziona il tuo nuovo wrappingMethod e accetta l'istanza NSArray per withObject.

A parte: Avere un wrapper qui può essere particolarmente utile se il tuo metodo originale prende uno o più tipi primitivi come input e quindi devi lavorare con NSNumber o NSStrings, ad esempio per soddisfare NSThread.

6

Questa è la parte superiore della mia testa, non testati:

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 

e quindi è possibile importare NSThread+ManyObjects e chiamare

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

Un aggiornamento per bella risposta di ennuikiller:

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

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