为什么这段代码会崩溃?

时间:2011-03-13 20:28:47

标签: iphone objective-c xml touchxml

我正在加载一个包含大约600个小数据集的XML file,大约有10,000行。数据用作地图上的注释点。我正在使用TouchXML库来处理XML。这是我的代码:

NSData *XMLData = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://skidmoreapps.com/darksky/fetch_info.php"]];
CXMLDocument *doc = [[[CXMLDocument alloc] initWithData:XMLData options:0 error:nil] autorelease];
NSArray *nodes = [doc nodesForXPath:@"//site" error:nil];

for (CXMLElement *node in nodes)
{
    ObservationSite *site = [ObservationSite mapAnnotation];
    [site setCoordinate:CLLocationCoordinate2DMake([[[[node elementsForName:@"lat"] objectAtIndex:0] stringValue] floatValue], [[[[node elementsForName:@"lng"] objectAtIndex:0] stringValue] floatValue])];
    [site setTitle:[[[node elementsForName:@"name"] objectAtIndex:0] stringValue]];
    [site setAddress:[[[node elementsForName:@"address"] objectAtIndex:0] stringValue]];
    [site setUrl:[[[node elementsForName:@"name_l"] objectAtIndex:0] stringValue]];
    [site setAffiliation:[[[node elementsForName:@"affil"] objectAtIndex:0] stringValue]];
    [site setAffiliationUrl:[[[node elementsForName:@"affil_l"] objectAtIndex:0] stringValue]];
    [site setOwner:[[[node elementsForName:@"owner"] objectAtIndex:0] stringValue]];
    [site setFee:[[[node elementsForName:@"fee"] objectAtIndex:0] stringValue]];
    [site setAccess:[[[node elementsForName:@"access"] objectAtIndex:0] stringValue]];
    [site setSky:[[[node elementsForName:@"sky"] objectAtIndex:0] stringValue]];
    [site setWeatherUrl:[[[node elementsForName:@"wx_l"] objectAtIndex:0] stringValue]];
    [site setPads:[[[node elementsForName:@"pads"] objectAtIndex:0] stringValue]];
    [site setParking:[[[node elementsForName:@"parking"] objectAtIndex:0] stringValue]];
    [site setRestrooms:[[[node elementsForName:@"b_rooms"] objectAtIndex:0] stringValue]];
    [site setSleep:[[[node elementsForName:@"sleep"] objectAtIndex:0] stringValue]];
    [site setNotes:[[[node elementsForName:@"notes"] objectAtIndex:0] stringValue]];

    [map addAnnotation:site];
}

下载XML文件后,它会因此错误崩溃:

1 个答案:

答案 0 :(得分:2)

从屏幕截图中我可以看到发生了mutateError。如果您修改了枚举的内容,则可能会在快速枚举期间发生这种情况。是否会发生任何会在快速枚举运行的同时修改[self children]集合的内容?例如,有多个线程在做什么吗?

如果你继续调试器并允许完全抛出异常,你在控制台上看到了什么?

顺便说一句,你的.php脚本似乎正在返回一个无效的XML文档 - 就我的浏览器而言,有些字符不是有效的UTF8。也许不是问题,但值得修理和排除。