【发布时间】:2012-06-22 01:54:34
【问题描述】:
我遇到了这个问题,我在控制台中不断收到此错误:
* 由于未捕获的异常“NSRangeException”而终止应用程序,原因:“* -[__NSArrayI objectAtIndex:]: index 0 beyond bounds for empty array”
这是我从 XML 文档中检索数据的代码:
CXMLDocument *worldweather = [[CXMLDocument alloc]initWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://free.worldweatheronline.com/feed/weather.ashx?q=%@&format=xml&num_of_days=4&key=0ded69e02b171832121504",query]] 选项:0 错误:无];
weathercondition = [[[worldweather nodesForXPath:@"/data/current_condition/weatherIconUrl" error:nil] objectAtIndex:0] stringValue];
Xcode 使用的位置是 Williams, CA,发送的查询是
威廉姆斯+CA
当我将它插入我的 URL 时,它可以工作并且我得到了我的 XML 文件,但是为什么我在我的控制台中得到这个消息???
这快把我逼疯了。非常感谢所有帮助!提前致谢!
【问题讨论】:
-
让我知道如果你执行 NSLog(@"class: %@", [[worldweather nodesForXPath:@"/data/current_condition/weatherIconUrl" error:nil] class]);
-
这是我现在的控制台:2012-06-19 20:17:38.441 Forecaster[10795:c07] Williams+CA 2012-06-19 20:17:38.799 Forecaster[10795:630b] () 2012-06-19 20:17:38.799 Forecaster[10795:630b] 数组中的元素:0 2012-06-19 20:17:38.800 Forecaster[10795:630b] *** 由于未捕获的异常“NSRangeException”而终止应用程序',原因:'*** -[__NSArrayI objectAtIndex:]:空数组的索引 0 超出范围'
-
我知道,但是在你有天气条件之前添加 = ... 只需添加我的行 NSLog(@"class: %@", [[worldweather nodesForXPath:@"/data/current_condition/weatherIconUrl"错误:无] 类]);让我知道控制台现在输出什么
-
我的错!对于那个很抱歉。这里是:2012-06-19 20:58:35.975 Forecaster[11460:5f0b] 类:__NSArrayI
-
好的,现在用这个替换那个 NSLog: NSLog(@"array returned: %@", [worldweather nodesForXPath:@"/data/current_condition/weatherIconUrl" error:nil]);跨度>
标签: objective-c ios xml xml-parsing touchxml