[英]Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'data parameter is nil'
I was developing apps using web services when I run the application it got responses but some time its crashes and show error like this: 运行应用程序时,我正在使用Web服务开发应用程序,但它得到了响应,但有时会崩溃并显示如下错误:
NSInvalidArgumentException', reason: 'data parameter is nil'
NSInvalidArgumentException”,原因:“数据参数为nil”
Here is my coding: 这是我的编码:
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];
}
I suspect the "headline"
object isn't always available; 我怀疑
"headline"
对象并不总是可用; guard against that using: 防止使用以下内容:
for (int i=0; i<[headlinetop count]; i++)
{
NSString *headstr=[[headlinetop objectAtIndex:i]objectForKey:@"headline"];
if (headstr)
[loadingcell addObject:headstr];
}
If loadcell
is an Objective-C collection class, it will object to headstr
being nil
as they cannot store NULL objects. 如果
loadcell
是Objective-C集合类,则它将反对headstr
为nil
因为它们不能存储NULL对象。
The problem is this code: 问题是此代码:
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&err];
NSDictionary *jsonArray=[NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:&err];
Sending a URL request you should always expect that something went wrong. 发送URL请求时,您始终应该期望出现问题。 There are many reasons for it.
原因有很多。 In this case
responseData
is nil
. 在这种情况下,
responseData
为nil
。 NSJSONSerialization
expects the data
arg not to be nil
: Error. NSJSONSerialization
期望data
arg不为nil
:错误。
When receiving a response for a request you should always check it for being nil
. 收到请求响应时,应始终检查它是否为
nil
。 Always. 总是。
So the code should look like this: 因此,代码应如下所示:
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&err];
if (responseData == nil)
{
// Maybe logging. But getting nil as a response of a URL request isn't exciting.
return; // or whatever you have to (not) do
}
NSDictionary *jsonArray=[NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:&err];
I think your problem is Url which you are using. 我认为您的问题是您正在使用的网址。 Please check the following link: 'NSInvalidArgumentException', reason: 'data parameter is nil'
请检查以下链接: “ NSInvalidArgumentException”,原因:“数据参数为nil”
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.