Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/101.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将XML解析为iOS_Ios_Objective C_Xml - Fatal编程技术网

将XML解析为iOS

将XML解析为iOS,ios,objective-c,xml,Ios,Objective C,Xml,我正在尝试将XML提要引入我的iOS应用程序,以便用户可以阅读最近的帖子。但我每次点击手机都会遇到这个错误 由于未捕获异常“NSInvalidArgumentException”而终止应用程序,原因:“尝试使用appendString变异不可变对象:” 代码如下,如果有人能帮我,那就太棒了 - (void)viewDidLoad { [super viewDidLoad]; self.feeds = [[NSMutableArray alloc] init]; NSUR

我正在尝试将XML提要引入我的iOS应用程序,以便用户可以阅读最近的帖子。但我每次点击手机都会遇到这个错误

由于未捕获异常“NSInvalidArgumentException”而终止应用程序,原因:“尝试使用appendString变异不可变对象:”

代码如下,如果有人能帮我,那就太棒了

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

}
您应该知道,在代码的这一点上会突出显示错误:

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

}

我希望在评论部分我们几乎解决了您的问题

从我看到的标题xml数据中,您会遇到一个CDDATA块。这是一种特殊类型的数据,您必须对其进行处理。您可以尝试此方法来挖掘数据

- (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock
{
    self.title = [[NSMutableString alloc] initWithData:CDATABlock encoding:NSUTF8StringEncoding];
}
您可以通过此链接了解有关清除CDDATA的更多信息
self.title
是一个
NSString
。您不能在
NSString
上调用
appendString:
。顺便说一句-您的
title
属性设置是否具有
copy
属性?我应该将其更改为什么?@dereksaundersNSMutableString@dereksaunders您应该将
copy
更改为
strong
,您的问题将得到解决。非常感谢您的帮助!我们离它越来越近了,它现在显示项目,但它显示的是博客文章的作者,我正试图显示标题。