ASIHTTPRequest:尝试调用我的委托方法时出错

时间:2010-08-17 16:46:24

标签: iphone objective-c asihttprequest

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

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

在循环中我向Queue添加了请求,在循环外添加了call [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]];
  

[invocation setArgument:& data atIndex:3];抛出异常,错误   消息是'NSInvalidArgumentException',原因:'*** - [NSInvocation   setArgument:atIndex:]:index(3)超出范围[-1,2]'

我做错了什么?

由于  `

1 个答案:

答案 0 :(得分:1)

问题在于:

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

在这里:

- (void)submitReportRequestDone:(ASIHTTPRequest *)request


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

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


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