performSelectorInBackground with multiple params

后端 未结 4 1888
孤街浪徒
孤街浪徒 2020-12-14 09:56

How can I call a method with multiple params like below with performSelectorInBackground?

Sample method:

-(void) reloadPage:(NSInteger)pageIndex fir         


        
4条回答
  •  感情败类
    2020-12-14 10:36

    with performSelectorInBackground you can only pass one argument, so make a custom object for this method to hold your data, itll be more concise than an ambiguous dictionary or array. The benefit of this is you can pass the same object around when done containing several return properties.

    #import 
    
    @interface ObjectToPassToMethod : NSObject
    
    @property (nonatomic, strong) NSString *inputValue1;
    @property (nonatomic, strong) NSArray *inputArray;
    @property (nonatomic) NSInteger returnValue1;
    @property (nonatomic) NSInteger returnValue2;
    
    @end
    

    and pass that object to your method:

    ObjectToPassToMethod *obj = [[ObjectToPassToMethod alloc] init];
    obj.inputArray = @[];
    obj.inputValue1 = @"value";
    [self performSelectorInBackground:@selector(backgroundMethod:) withObject:obj];
    
    
    -(void)backgroundMethod:(ObjectToPassToMethod*)obj
    {
        obj.returnValue1 = 3;
        obj.returnValue2 = 90;
    }
    

    make sure to clean up the object when done to prevent memory leaks

提交回复
热议问题