从RSS源解析摘要会导致应用崩溃

时间:2014-05-16 23:35:59

标签: ios uitableview ios7 nsxmlparser

我正在尝试向正在解析RSS Feed的UITableView添加摘要。我已经设置了一个方法来获取摘要,但应用程序崩溃了。 受害者是:

[item setObject:summary forKey:@"summary"];

崩溃代码:

2014-05-17 09:32:09.231 ***[1169:90b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** setObjectForKey: object cannot be nil (key: summary)'
*** First throw call stack:
(
    0   CoreFoundation                      0x0453a1e4 __exceptionPreprocess + 180
    1   libobjc.A.dylib                     0x037bd8e5 objc_exception_throw + 44
    2   CoreFoundation                      0x045c3eb8 -[__NSDictionaryM setObject:forKey:] + 888
    3   ***                                 0x0010367a -[*** parser:didEndElement:namespaceURI:qualifiedName:] + 394
    4   Foundation                          0x034ac991 _endElementNs + 363
    5   libxml2.2.dylib                     0x00626788 xmlParseEndTag2 + 744
    6   libxml2.2.dylib                     0x00628bf8 xmlParseTryOrFinish + 3347
    7   libxml2.2.dylib                     0x00627cfa xmlParseChunk + 886
    8   Foundation                          0x034aa8fb -[NSXMLParser parseData:] + 329
    9   Foundation                          0x034aac17 -[NSXMLParser parseData:] + 1125
    10  Foundation                          0x034aadf1 -[NSXMLParser parseFromStream] + 287

主要代码:

 - (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {

        if ([elementName isEqualToString:@"item"]) {

            [item setObject:title forKey:@"title"];
            [item setObject:link forKey:@"link"];
    [item setObject:summary forKey:@"summary"];
            [feeds addObject:[item copy]];

        }

    }
    - (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];
        link    = [[NSMutableString alloc] init];
summary = [[NSMutableString alloc] init];

    }

}
    - (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {

        if ([element isEqualToString:@"title"]) {
            [title appendString:string];
        } else if ([element isEqualToString:@"link"]) {
            [link appendString:string];
        }else if ([element isEqualToString:@"summary"]) {
            [summary appendString:string];
        }

    }

问题是什么?我可以理解代码是说没有返回,所以我应该放什么而不是摘要?

干杯,SebOH

1 个答案:

答案 0 :(得分:1)

您的问题在于didStartElement方法。当您检测到item时,您需要将摘要初始化为空的NSString @“” - 否则它将为零并且您将继续在nil上调用appendString,从而导致零值。

应该是

- (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];
        link    = [[NSMutableString alloc] init];
        summary = [[NSMutableString alloc] init];

    }
}