我编写了以下代码来解析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];
}
}
答案 0 :(得分:1)
我认为错误在于这一行:
element = elementName;
你必须初始化变量元素。写下这个:
element = [[NSString alloc] initWithString:elementName];
然后它应该工作。