-[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];
}发布于 2012-08-29 01:25:43
你可以发送一个字典,里面有你想要的所有值
发布于 2012-08-29 01:26:42
您可以使用NSInvocation对象来初始化操作:
- (id)initWithInvocation:(NSInvocation *)inv(NSInvocation可以处理多个参数);或者,您可以将所需的参数包装到NSArray或NSDictionary中,前提是它们是对象。
发布于 2012-08-29 01:39:02
在将操作添加到队列之前,询问操作的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,它更加灵活和易于使用。
https://stackoverflow.com/questions/12164400
复制相似问题