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];
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!
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
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.
I think the app crash at offline mode and you tried to NSJSONSerialization the data. Thanks
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With