-[NSInvocationOperation initWithTarget:selector:object:]
只接受一個要作爲將被調用的方法的參數傳遞的對象。我想用兩個參數;我怎樣才能做到這一點?爲NSInvocationOperation設置多個參數
這是我的代碼:
- (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];
}
@ H2CO3的答案是完全正確的,但我強烈建議不要使用'NSInvocationOperation',除非你對它有強烈的需求。 'NSBlockOperation'生成速度更快,編碼更容易,而且更靈活。調用創建起來非常昂貴,並且已經被塊廣泛地取代。 –