10
votes

XCode newbie here and i have been trying to figure out why the following is happening...

With the way the code is currently written, i keep getting 'NSInvalidArgumentException'.

reason: 'data parameter is nil' in xcode. The url works fine on the browser.

When I remove the "filters=%7B%22region%22%3A%22CA%22%7D" part of the url, it works fine in Xcode, but when this section of the url is included, that's when I get the error message. I have tried using \" in replacement of the %22 but still nothing. Any suggestions are greatly appreciated.

NSURL *url = [NSURL URLWithString:[@"http://api.v3.factual.com/t/restaurants-us?q=peets+coffee&filters=%7B%22region%22%3A%22CA%22%7D&KEY=p7kwKMFUSyVi64FxnqWmeSDEI41kzE3vNWmwY9Zi"stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSData *data = [NSData dataWithContentsOfURL: url];
6
What is actual value for "filters=%7B%22region%22%3A%22CA%22%7D"?Nirmalsinh Rathod

6 Answers

7
votes

I also had the same problem:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'data parameter is nil'

But I had solved it. The reason is my API URL is incorrect. Please check out your API URL. Good luck for you!

6
votes

The code you have posted works without crashing, but the data object ends up being nil

At some point later in the code you access data. Since data == nil, the app crashes.

I suggest entering

http://api.v3.factual.com/t/restaurants-us?q=peets+coffee&filters=%7B%22region%22%3A%22CA%22%7D&KEY=p7kwKMFUSyVi64FxnqWmeSDEI41kzE3vNWmwY9Zi

and

http://api.v3.factual.com/t/restaurants-us?q=peets+coffee&KEY=p7kwKMFUSyVi64FxnqWmeSDEI41kzE3vNWmwY9Zi

into a browser to shed some light on the situation

UPDATE

The problem was encoding already encoded strings. (The %'s are being encoded to %25)

NSString *urlBase = @"http://api.v3.factual.com/t/restaurants-us?";
NSString *urlData = [@"q=peets+coffee&filters={\"region\":\"CA\"}&KEY=p7kwKMFUSyVi64FxnqWmeSDEI41kzE3vNWmwY9Zi"stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url=[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",urlBase,urlData]];
NSData* data = [NSData dataWithContentsOfURL: url];

Works, because you are encoding a non-encoded string.

OR

NSURL *url=[NSURL URLWithString:[NSString stringWithFormat:@"http://api.v3.factual.com/t/restaurants-us?q=peets+coffee&filters=%7B%22region%22%3A%22CA%22%7D&KEY=p7kwKMFUSyVi64FxnqWmeSDEI41kzE3vNWmwY9Zi"]];
NSData* data = [NSData dataWithContentsOfURL: url];

Will work because as your post is, your string was already encoded properly

1
votes

I had the same issue and I found one word in service name in upper case while actually it was in lower on server. I did fixed the name in URL and issue got resolved. Thanks.

1
votes

I think the app crash at offline mode and you tried to NSJSONSerialization the data. Thanks

1
votes

In my case, I forgot to add App Transport Security Settings in the .plist.

So just go to the .plist and add App Transport Security Settings, and then Allow Arbitrary Loads to yes.

0
votes

I had the same problem. I found out that error was produced because the URL I was using to download some stuff using NSURLSession had become invalid. Removing the operation solved the problem for me.