'Attempt to mutate immutable object with appendString:' 解析时出错 xml

'Attempt to mutate immutable object with appendString:' error when parsing xml

我正在使用 NSXMLParser 解析 Objective-C 中的 rss 提要。 此 rss 类似于:

<?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>

我的 NSXML 代表是这样的:

    - (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);

}

我已经声明了所有需要的变量。正如您可能已经看到的,我正在尝试解析以 rss 元素的属性形式呈现的 link。在我这样做之前,该应用程序运行良好,但出现图像 imageLink = [attributeDict objectForKey:@"url"]; "Attempt to mutate immutable object with appendString:" 错误。

大家怎么看?

由于您使用了 NSMutableString,因此您应该替换下面的代码

if([element isEqualToString:@"media"]){
    imageLink = [[NSMutableString alloc] init];
    imageLink = [attributeDict objectForKey:@"url"];
   }

 if([element isEqualToString:@"media"]){
       imageLink = [[NSMutableString alloc] init];
      [imageLink appendString:[attributeDict objectForKey:@"url"]];
    }