Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios NSData dataWithContentsOfURL:返回零_Ios_Json_Parsing_Url_Nsdata - Fatal编程技术网

Ios NSData dataWithContentsOfURL:返回零

Ios NSData dataWithContentsOfURL:返回零,ios,json,parsing,url,nsdata,Ios,Json,Parsing,Url,Nsdata,运行此代码后,我的数据变量为nil。如果您遵循链接,您将看到它是一个JSON文件。我已经在本地使用完全相同的文件运行了该函数,但它无法在给定的URL上无误地获取数据。不确定为什么要嵌套对URLWithString: NSString *jsonPath = [[NSBundle mainBundle] pathForResource:@"CRN_JSON" ofType:@"json"];

运行此代码后,我的数据变量为nil。如果您遵循链接,您将看到它是一个JSON文件。我已经在本地使用完全相同的文件运行了该函数,但它无法在给定的URL上无误地获取数据。

不确定为什么要嵌套对
URLWithString:

NSString *jsonPath = [[NSBundle mainBundle] pathForResource:@"CRN_JSON"
                                                     ofType:@"json"];


dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
    data = [NSData dataWithContentsOfURL:
                    [NSURL URLWithString:@"http://properfrattire.com/Classifi/CRN_JSON.json"]];
    [self performSelectorOnMainThread:@selector(fetchedData:)
                           withObject:data waitUntilDone:YES];

});
一次就可以了:

[NSURL URLWithString:[NSURL URLWithString:@"http://properfrattire.com/Classifi/CRN_JSON.json"]]];

另外,您应该使用
dataWithContentsOfURL:options:error:
,这样您就可以看到任何错误。

并不是说有错误。只是您希望异步调用的行为与同步调用一样。您能解释一下这是什么意思吗?您希望传递给dispatch_async的块中的命令序列按顺序执行,并在函数返回后完成。但是,由于它是异步的,所以不会发生这种情况-函数在块完成运行之前返回。这里面有什么不明显的?您没有阅读
dispatch\u async()
的文档吗?我只是在学习本教程,我对本教程还不熟悉,不完全了解需要做哪些更改才能使其正常工作。你能具体说明我需要更改什么吗?问题是你在哪里检查数据内容。如果它正好在
dispatch\u async()
调用之后,它将是零,因为网络代码还没有时间完成。网络代码与主代码并行运行。这就是异步的意思。那么你在哪里检查数据呢?对不起,是的,我只是在这里输入了错误的数据,这就是我实际拥有的数据,但它仍然不起作用。@BryceLanglotz问问题时,粘贴真实代码很重要。输入假代码会使查找问题变得更加困难。
[NSURL URLWithString:@"http://properfrattire.com/Classifi/CRN_JSON.json"];