运行应用程序时,我正在使用Web服务开发应用程序,但该应用程序得到响应,但有时会崩溃并显示如下错误:


  NSInvalidArgumentException”,原因:“数据参数为nil”


这是我的编码:

NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://api.espn.com/v1/sports/tennis/news/headlines?apikey=th98vzm67pufc36ka42xxxmy"]];
[request setHTTPMethod:@"GET"];
[request setValue:@"application/json;charset=UTF-8" forHTTPHeaderField:@"content-type"];

NSError *err;
NSURLResponse *response;

NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&err];
NSDictionary *jsonArray=[NSJSONSerialization JSONObjectWithData:responseData  options:NSJSONReadingMutableContainers error:&err];
NSArray *headlinetop=[jsonArray objectForKey:@"headlines"];

for (int i=0; i<[headlinetop count]; i++)
{
    NSString *headstr=[[headlinetop objectAtIndex:i]objectForKey:@"headline"];
    [loadingcell addObject:headstr];
}

最佳答案

我怀疑对象并非总是可用;防止使用:

for (int i=0; i<[headlinetop count]; i++)
{
    NSString *headstr=[[headlinetop objectAtIndex:i]objectForKey:@"headline"];
    if (headstr)
        [loadingcell addObject:headstr];
}


如果"headline"是Objective-C集合类,则它将作为loadcell反对headstr,因为它们无法存储NULL对象。

10-02 21:07