performSelectorInBackground具有多个参数 [英] performSelectorInBackground with multiple params
问题描述
如何使用performSelectorInBackground调用具有多个参数的方法?
How can I call a method with multiple params like below with performSelectorInBackground?
示例方法:
-(void) reloadPage:(NSInteger)pageIndex firstCase:(BOOL)firstCase;
推荐答案
问题是 performSelectorInBackground :withObject:
只需要一个对象参数。解决此限制的一种方法是将参数的字典(或数组)传递给解包参数并调用实际方法的包装器方法:
The problem is that performSelectorInBackground:withObject:
takes only one object argument. One way to get around this limitation is to pass a dictionary (or array) of arguments to a "wrapper" method that deconstructs the arguments and calls your actual method:
- (void)callingMethod {
NSDictionary * args = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithInteger:pageIndex], @"pageIndex",
[NSNumber numberWithBool:firstCase], @"firstCase",
nil];
[self performSelectorInBackground:@selector(reloadPageWrapper:)
withObject:args];
}
- (void)reloadPageWrapper:(NSDictionary *)args {
[self reloadPage:[[args objectForKey:@"pageIndex"] integerValue]
firstCase:[[args objectForKey:@"firstCase"] boolValue]];
}
- (void)reloadPage:(NSInteger)pageIndex firstCase:(BOOL)firstCase {
// Your code here...
}
这种方式你只是传递一个单一参数到后台调用,但该方法可以构造您需要的多个参数真正的调用(这将发生在同一个后台线程)。
This way you're only passing a "single" argument to the backgrounding call, but that method can construct the multiple arguments you need for the real call (which will take place on the same backgrounded thread).
这篇关于performSelectorInBackground具有多个参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!