-[NSInvocationOperation initWithTarget:selector:object:]
只接受一个对象作为要调用的方法的参数传递。我想用两个论点;我怎么能这样做?
这是我的代码:
- (void)loadImage:(NSURL *)imageURL
{
NSOperationQueue *queue = [NSOperationQueue new];
NSInvocationOperation *operation = [[NSInvocationOperation alloc]
initWithTarget:self
selector:@selector(requestRemoteImage:)
object:imageURL];
[queue addOperation:operation];
}
- (void)requestRemoteImage:(NSURL *)imageURL
{
NSData *imageData = [[NSData alloc] initWithContentsOfURL:imageURL];
UIImage *image = [[UIImage alloc] initWithData:imageData];
[self performSelectorOnMainThread:@selector(placeImageInUI:) withObject:image waitUntilDone:YES];
}
答案 0 :(得分:1)
您可以使用NSInvocation对象初始化opertation:
- (id)initWithInvocation:(NSInvocation *)inv
(NSInvocation可以处理多个参数);或者您可以将所需的参数包装到NSArray或NSDictionary中,前提是它们是对象。
答案 1 :(得分:1)
询问操作invocation
并在将操作添加到队列之前修改它。
NSInvocation * invocation = [operation invocation];
[invocation setArgument:&anotherObject atIndex:3];
// Indexes 0 and 1 are self and cmd, and you've already used index 2
或者as Rob Napier suggested,使用NSBlockOperation
,它更灵活,更易于使用。
答案 2 :(得分:-1)
您可以发送包含所需值的词典