NSXMLParser代码的EXC_BAD_ACCESS

时间:2014-04-30 07:25:25

标签: ios objective-c xml-parsing exc-bad-access nsxmlparser

我编写了以下代码来解析RSS feeds.,但是当我尝试将元素与EXC_BAD_ACCESS进行比较时,我一直在findCharacter方法中获取@"title"。我知道哪里出错了?< / p>

NSMutableDictionary * item;
NSMutableString * title;
NSMutableString * link;
NSString *element;
NSMutableArray *feeds; 

-(void) parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{
    element = elementName;
    if ([element isEqualToString:@"item"]) {
        item = [[NSMutableDictionary alloc]init];
        title = [[NSMutableString alloc]init];
        link = [[NSMutableString alloc] init];
    }
}

-(void) parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{
    if ([elementName isEqualToString:@"item"]) {
        [item setObject:title forKey:@"title"];
        [item setObject:link forKey:@"link"];
        [feeds addObject:[item copy]];
        NSLog(@"feeds :: %@",feeds);
    }


}

-(void) parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
    if ([element isEqualToString:@"title"]) {
        [title appendString:string];`enter code here`
    }else if ([element isEqualToString:@"link"]){
        [link appendString:string];
    }
}

1 个答案:

答案 0 :(得分:1)

我认为错误在于这一行:

element = elementName;

你必须初始化变量元素。写下这个:

element = [[NSString alloc] initWithString:elementName];

然后它应该工作。