0

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];
    }

}
rayryeng
  • 102,964
  • 22
  • 184
  • 193

1 Answers1

0

i hope in the comment section we have almost solve your problem.

from the xml data i see for title you encounter a CDDATA block. this is a special type of data and you have to process that. you can try this to grub the data....

- (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock
{
    self.title = [[NSMutableString alloc] initWithData:CDATABlock encoding:NSUTF8StringEncoding];
}

You can follow this link to learn more about grubbing CDDATA NSXMLParser doesn't ignore CDATA

Community
  • 1
  • 1
NULL
  • 1,848
  • 1
  • 21
  • 23
  • thank you so much for your help! We are getting closer it now displays items, but it displays the author of the blog post and Im trying to display the title. – dereksaunders Jul 09 '14 at 02:02