ios “NSInvalidArgumentException”的,原因是:“数据参数是零”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9603708/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 17:12:11  来源:igfitidea点击:

'NSInvalidArgumentException', reason: 'data parameter is nil'

iosobjective-ciphone

提问by user1254909

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

这里是 XCode 新手,我一直在试图弄清楚为什么会发生以下情况......

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

按照当前编写代码的方式,我不断收到“NSInvalidArgumentException”。

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

原因:xcode 中的“数据参数为零”。该网址在浏览器上运行良好。

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.

当我删除 url 的 "filters=%7B%22region%22%3A%22CA%22%7D" 部分时,它在 Xcode 中工作正常,但是当包含 url 的这一部分时,我就会收到错误消息. 我曾尝试使用 \" 代替 %22 但仍然没有任何建议。不胜感激。

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];

回答by luofubin

I also had the same problem:

我也遇到了同样的问题:

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

由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“数据参数为零”

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

但我已经解决了。原因是我的 API URL 不正确。请检查您的 API URL。祝你好运!

回答by Jesse Black

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

您发布的代码可以正常工作而不会崩溃,但数据对象最终被 nil

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

稍后在您访问的代码中的某个时间点data。从那以后data == nil,应用程序崩溃了。

I suggest entering

我建议进入

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

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

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)

问题是编码已经编码的字符串。(%'s 被编码为 %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

会起作用,因为就像您的帖子一样,您的字符串已经正确编码

回答by Sultan Ali

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

就我而言,我忘了补充App Transport Security Settings.plist

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

因此,只需转到.plist并添加App Transport Security Settings,然后Allow Arbitrary Loads转到yes

回答by Sid

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.

我遇到了同样的问题,我在服务名称中发现一个大写的单词,而实际上它在服务器上的小写。我确实在 URL 中修复了名称,问题得到了解决。谢谢。

回答by Fadia Jaradat

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

我认为应用程序在离线模式下崩溃,并且您尝试对数据进行 NSJSONSerialization。谢谢

回答by Septronic

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.

我有同样的问题。我发现这个错误是因为我用来使用 NSURLSession 下载一些东西的 URL 变得无效。删除操作为我解决了问题。