NSOperation的倍数参数?

时间:2012-03-30 00:27:56

标签: iphone cocoa-touch nsoperation nsoperationqueue nsinvocationoperation

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

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

2 个答案:

答案 0 :(得分: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中,您可以使用存储在该字典中的对象和值。

答案 1 :(得分:1)

//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];