calling selector with two arguments on NSThread issue

后端 未结 4 1849
情深已故
情深已故 2020-12-31 05:27

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

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


        
相关标签:
4条回答
  • 2020-12-31 05:39

    Wrap your selected method with an auxiliary wrapping method, "wrappingMethod", that processes an NSArray of inputs to suit your own method before calling your own method within wrappingMethod. Now detach an NSThread that selects your all new wrappingMethod and takes the NSArray instance for withObject.

    Aside: Having a wrapper here can be particularly helpful if your original method takes one or more primitive types as input and you then have to work with NSNumber or NSStrings, say, to satisfy NSThread.

    0 讨论(0)
  • 2020-12-31 05:50

    An update to ennuikiller's nice answer:

    NSDictionary* params = [NSDictionary dictionaryWithObjectsAndKeys:@"IMG_URL_VALUE",@"img_url",@"PARAM2_VALUE", @"param2", nil];
    
    [NSThread detachNewThreadSelector:@selector(loadImage:) toTarget:self withObject:params];
    
    0 讨论(0)
  • 2020-12-31 05:51

    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]; 
    
    0 讨论(0)
  • 2020-12-31 05:54

    This is off the top of my head, untested:

    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
    

    And then you can import NSThread+ManyObjects and call

    [NSThread detachNewThreadSelector:@selector(loginWithUser:user:password:) toTarget:self withObject:@"someusername" andObject:@"somepassword"];
    
    0 讨论(0)
提交回复
热议问题