1

我有一个 ASINetworkQueue 实例,并将 ASIHTTPRequest 实例添加到队列中;同时,我为队列以及每个请求设置了委托方法:

[submittingReportQueue setDelegate:self];
[submittingReportQueue setRequestDidFailSelector:@selector(submitReportQueueWentWrong:)];
[submittingReportQueue setQueueDidFinishSelector:@selector(submitReportQueueFinished:)];

在一个循环中,我向队列添加了请求,在循环之外添加了调用 [submittingReportQueue go]。

ASIHTTPRequest *request = [[[ASIHTTPRequest alloc] initWithURL:url] autorelease];
 NSString *auth = [[AuthenticationManager sharedInstance] authenticationHeaderValue];
 request addRequestHeader:@"Authorization" value:auth];
 [request addRequestHeader:@"Content-Type" value:@"application/json"];
 NSString *jsonString =[self jsonStringForExpenseReport:report];
 [request appendPostData:[jsonString dataUsingEncoding:NSUTF8StringEncoding]];
 [request setDelegate:self];
 [request setUserInfo:[NSDictionary dictionaryWithObject:report forKey:@"Report"]];
 [request setDidFailSelector:@selector(submitReportRequestWentWrong:)];
 [request setDidReceiveDataSelector:@selector(submitReportRequestDone:)];
 [requests addObject:request];
 [submittingReportQueue addOperation:request];

以下是我的委托方法:

- (void)submitReportQueueWentWrong:(ASINetworkQueue *)queue
{
    NSLog(@"Submit Report WentWRong");

//
- (void)submitReportQueueFinished:(ASINetworkQueue *)queue
{
    NSLog(@"Submit Report QueueFinished");

}
//
- (void)submitReportRequestWentWrong:(ASIHTTPRequest *)request
{
        NSLog(@"Submit Report Queue went wrong");
}

//
- (void)submitReportRequestDone:(ASIHTTPRequest *)request

{//do work here}

但是 ASIHTTPRequest.m 在以下代码块中抛出异常:

// Does the delegate want to handle the data manually?
if ([[self delegate] respondsToSelector:[self didReceiveDataSelector]]) {
  NSMethodSignature *signature = [[[self delegate] class]     
           instanceMethodSignatureForSelector:[self didReceiveDataSelector]];
  NSInvocation *invocation = [[NSInvocation invocationWithMethodSignature:signature] 
           retain];
 [invocation setSelector:[self didReceiveDataSelector]];
 [invocation setArgument:&self atIndex:2];
 NSData *data = [NSData dataWithBytes:buffer length:bytesRead];
 [invocation setArgument:&data atIndex:3];
 [invocation retainArguments];
 [self performSelectorOnMainThread:@selector(invocateDelegate:) withObject:invocation waitUntilDone:[NSThread isMainThread]];

[调用 setArgument:&data atIndex:3]; 抛出异常,错误消息是 'NSInvalidArgumentException',原因:'*** -[NSInvocation setArgument:atIndex:]: index (3) out of bounds [-1, 2]'

我做错了什么?

谢谢`

4

1 回答 1

1

问题在这里:

[request setDidReceiveDataSelector:@selector(submitReportRequestDone:)];

和这里:

- (void)submitReportRequestDone:(ASIHTTPRequest *)request


可能不是您想要的didReceiveDataSelector,因为每次收到一大块数据时都会调用它 - 我怀疑您希望在请求完成时被调用,因此您应该设置 requestDidFinish 选择器:

[request setRequestDidFinishSelector:@selector(submitReportRequestDone:)];


对于您的信息,您看到的错误基本上意味着“您给我的选择器没有正确的调用签名”或更具体地说“我正在尝试调用一个采用 2 个参数的方法,而您的方法只需要 1 ”。(如果我没记错的话,它在索引 3 上失败的原因是第一个参数是包含对象的隐藏参数。)

于 2010-08-17T20:43:42.147 回答