I am trying to bring in an XML feed into my iOS app so the user can read recent posts. But I keep getting this error everytime I click on the cell.
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Attempt to mutate immutable object with appendString:'
The code is as follows, if anyone can help me out that'd be awesome!
- (void)viewDidLoad
{
[super viewDidLoad];
self.feeds = [[NSMutableArray alloc] init];
NSURL *url = [NSURL URLWithString:@"http://www.autoblog.com/rss.xml"];
self.parser = [[NSXMLParser alloc] initWithContentsOfURL:url];
[self.parser setDelegate:self];
[self.parser setShouldResolveExternalEntities:NO];
[self.parser parse];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"posts" forIndexPath:indexPath];
// Configure the cell...
cell.textLabel.text = [[self.feeds objectAtIndex:indexPath.row] objectForKey: @"title"];
return cell;
}
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict {
self.element = elementName;
if ([self.element isEqualToString:@"item"]) {
self.item = [[NSMutableDictionary alloc] init];
self.title = [[NSMutableString alloc] init];
self.link = [[NSMutableString alloc] init];
}
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
if ([self.element isEqualToString:@"title"]) {
[self.title appendString:string];
} else if ([self.element isEqualToString:@"link"]) {
[self.link appendString:string];
}
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
if ([elementName isEqualToString:@"item"]) {
[self.item setObject:self.title forKey:@"title"];
[self.item setObject:self.link forKey:@"link"];
[self.feeds addObject:[self.item copy]];
}
}
- (void)parserDidEndDocument:(NSXMLParser *)parser {
[self.tableView reloadData];
}
Just so you know the error is highlighted at this point of code:
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
if ([self.element isEqualToString:@"title"]) {
[self.title appendString:string];
} else if ([self.element isEqualToString:@"link"]) {
[self.link appendString:string];
}
}