I'm parsing an rss feed in Objective-C with NSXMLParser. This rss is similar to this:
<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:media="http://search.yahoo.com/mrss/">
<channel>
<title>My RSS</title>
<description>My Rss parser</description>
<item>
<title>Some kind of a title</title>
<description>
<![CDATA[]]>
</description>
<media:thumbnail url="http://www.example.org/pics/Thumbs/picture.jpg"/>
<pubDate>Tue, 20 Jan 2015 22:24:56 -0500</pubDate>
</item>
</channel>
</rss>
And my NSXML delegate is this:
- (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];
pubDate = [[NSMutableString alloc] init];
}
if([element isEqualToString:@"media"]){
imageLink = [[NSMutableString alloc] init];
imageLink = [attributeDict objectForKey:@"url"];
}
NSLog (@"Parse did Start");
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
if ([element isEqualToString:@"title"]) {
[title appendString:string];
} else if ([element isEqualToString:@"media"]) {
imageLink = string;
} else if ([element isEqualToString:@"pubDate"]) {
[pubDate appendString:string];
}
NSLog(@"Found characters");
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
if ([elementName isEqualToString:@"item"]) {
[item setObject:title forKey:@"title"];
[item setObject:imageLink forKey:@"media"];
[item setObject:pubDate forKey:@"pubDate"];
[feeds addObject:[item copy]];
}
NSLog(@"Did end elements");
}
- (void)parserDidEndDocument:(NSXMLParser *)parser {
[self.tableView reloadData];
NSLog(@"Parsing is done");
NSLog(@"%@", title);
}
I have already declared all the needed variables. As you might have seen I'm trying to parse the link presented in a form of attribute for the element of the rss. Before I did this, the app was working fine but the image imageLink = [attributeDict objectForKey:@"url"];
"Attempt to mutate immutable object with appendString:" bug showed up.
What do you think guys?
As you had taken the the NSMutableString you should replace your below code
if([element isEqualToString:@"media"]){
imageLink = [[NSMutableString alloc] init];
imageLink = [attributeDict objectForKey:@"url"];
}
with
if([element isEqualToString:@"media"]){
imageLink = [[NSMutableString alloc] init];
[imageLink appendString:[attributeDict objectForKey:@"url"]];
}