1

我已经解析了一个 xml 并将解析的数据存储在属性声明、保留和合成的字符串上,这些值在解析过程中进入这些字符串,但在解析器完成解析后被无效。我无法从另一个 IBAction 方法访问它们,在那里我得到 EXEC BAD ACCESS 错误,根据我的经验,它认为当调试器访问无效的内存时会出现这种情况。

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{            
//NSLog(@"found this element: %@", elementName);
currentElement = [elementName copy];
if ([elementName isEqualToString:@"resp"]) {
    // clear out our story item caches...
}
else if ([currentElement isEqualToString:@"org"]) {
    name = [attributeDict objectForKey:@"fileid"];
    extention = [attributeDict objectForKey:@"extension"];
    NSLog(@"image named %@ of type %@",name,extention);
}
else if ([currentElement isEqualToString:@"photo"]) {
    photoid = [attributeDict objectForKey:@"photoid"];
    NSLog (@"photo id = %@",photoid);
}       

}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
    //NSLog(@"found characters: %@", string);
    // save the characters for the current item...
    if ([currentElement isEqualToString:@"userid"]) {
        userid =[NSString stringWithString:string];
        NSLog (@"user id = %@",userid);
        } 
    else if ([currentElement isEqualToString:@"albumid"]) {
        albumid =[NSString stringWithString:string];
        NSLog (@"album id = %@",albumid);
    }
}

这部分工作正常,但是

- (IBAction)download {
    NSString *urlstr = [NSString stringWithFormat:@"http://staging-data.mysite.com/%@/albumphoto/%@/%@/%@.%@",userid,albumid,photoid,name,extention];
    NSLog(urlstr);
    NSURL *url= [NSURL URLWithString:urlstr];
    NSData *imgdata = [[NSData alloc]initWithContentsOfURL:url];
    UIImage *img = [UIImage imageWithData:imgdata];
    newImage.image=img;
    [imgdata release];

}

这会导致错误。请帮忙

4

2 回答 2

1

我建议你仔细看看这一行:

name = [attributeDict objectForKey:@"fileid"];

它将一个属性设置为NSStringXML 解析器拥有的属性,而不复制它、保留它等。我建议您复制并保留它。

我担心的是,当 XML 解析器消失时,它会带上name字符串以及extensionand photoid

于 2010-01-11T07:23:16.363 回答
0

您还必须实现此方法才能知道何时遇到结束标记。

  • (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURIqualifiedName:(NSString *)qName
于 2010-01-11T07:17:45.243 回答