我正在通过 NSXMLParser 进行 XML 解析。但是当谈到 NSXMLParser 的 didendElement 时,它有时会崩溃并有时会给出结果。我不明白我哪里出错了?
arr3=[[NSMutableArray alloc] init];
drr3=[[NSMutableDictionary alloc]init];
NSURL *urlString2= [NSString stringWithFormat:@"http://www.XXX.YYY/services/SService.svc/Get"];
currentURL=@"1";
/// calling the service url and getting the repsnse in array (arr3)
if([arr3 count]==0)
{
currentURL=@"2";
//calling another service url and storing in arr3.
}
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict {
if(currentURL==@"1")
{
if([elementName isEqualToString:@"PWList"])
{
arr3=[[NSMutableArray alloc] init];
drr3=[[NSMutableDictionary alloc] init];
}
if([elementName isEqualToString:@"Name"])
{
drr3=[[NSMutableDictionary alloc] init];
}
srr3=[[NSMutableString alloc] init];
}
if(currentURL==@"2")
{
if([elementName isEqualToString:@"WeeksList"])
{
arr3=[[NSMutableArray alloc] init];
}
if([elementName isEqualToString:@"Person"])
{
drr3=[[NSMutableDictionary alloc]init];
}
srr3=[[NSMutableString alloc] init];
}
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
if(curentURL==@"1")
{
//some code
if([elementName isEqualToString:@"PWList"])
{
[drr3 release],drr3=nil;
}
}
if(curentURL==@"2")
{
if([elementName isEqualToString:@"Code"])
{
[drr3 setObject:srr3 forKey:@"Code"]; //crashes at this line
[srr3 release],srr3=nil;
return;
}
}
}
当涉及到[drr3 setObject:srr3 forKey:@"Code"];
给我错误“__NSDictionaryM setObject:forKey:]: message sent to deallocated instance".
有时我确实得到了结果。
我能做些什么来摆脱这个错误?