-[NSInvocationOperation initWithTarget:selector:object:]
은 호출 할 메서드에 대한 인수로 전달할 하나의 개체 만 허용합니다. 두 가지 주장을 사용하고 싶습니다. 내가 어떻게 할 수 있니?NSInvocationOperation에 대해 둘 이상의 인수를 설정하는 경우
- (id)initWithInvocation:(NSInvocation *)inv
: 당신은 당신이
-[NSInvocationOperation initWithTarget:selector:object:]
은 호출 할 메서드에 대한 인수로 전달할 하나의 개체 만 허용합니다. 두 가지 주장을 사용하고 싶습니다. 내가 어떻게 할 수 있니?NSInvocationOperation에 대해 둘 이상의 인수를 설정하는 경우
- (id)initWithInvocation:(NSInvocation *)inv
: 당신은 당신이
당신은 opertation를 초기화하는있는 NSInvocation 객체를 사용할 수 있습니다 원하는 모든 값으로 사전을 보낼 수
- (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];
}
작업에 해당 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
를 사용합니다.
requestRemoteImage 메서드에서 새 인수를 사용하고 싶습니다. –
@ H2CO3의 대답은 정확 합니다만 강력한 대응이 필요하지 않으면'NSInvocationOperation'을 사용하지 않는 것이 좋습니다. NSBlockOperation은 생성 속도가 빠르고 코드 작성이 쉽고 유연합니다. 호출은 매우 비싸고 블록으로 대체되었습니다. –