0

我在我的应用程序中使用 NSOperationQueue 并且我想为我的操作设置多个参数我该怎么做?

   NSOperationQueue *queue = [[[NSOperationQueue alloc] init] autorelease];
   NSInvocationOperation *operation = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(methodCall) object:nil];
  [queue addOperation:operation];
  [operation release];
4

2 回答 2

6

您必须使用所需的数据创建一个数组或字典。

前任:

NSOperationQueue *queue = [[[NSOperationQueue alloc] init] autorelease];
NSDictionary *argumentDictionary = [NSDictionary dictionaryWithObjectsAndKeys:object1, @"Object1Key", object2, @"Object2Key", nil];
NSInvocationOperation *operation = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(methodCall:) object:argumentDictionary];
[queue addOperation:operation];
[operation release];

并且- (void)methodCall:(NSDictionary *)argumentDictionary您可以使用存储在该字典中的对象和值。

于 2012-03-30T00:37:33.537 回答
2
//Correct approach is to use NSInvocation
//create nsinvocation obj
SEL selector= @selector(methodName:);
NSMethodSignature * sig= [[self class] instanceMethodSignatureForSelector: selector];
NSInvocation * invocation=[NSInvocation invocationWithMethodSignature:sig];
[invocation setTarget: self];   
[invocation setSelector:selector];
[invocation setArgument:&firstArgument atIndex: 2];
[invocation setArgument:&secArgument atIndex: 3];
//operation with invocation
NSInvocationOperation* operation = [[NSInvocationOperation alloc] initWithInvocation:invocation];
[opQueue addOperation:operation];
于 2014-11-11T14:04:55.220 回答