Search code examples
iosuitableviewios7nsxmlparser

Parsing summary from RSS Feed causes app to crash


I am trying to add a summary to a UITableView which is parsing a RSS Feed. I have set up a method to get the Summary, but the app is crashing. The victim line is:

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

Crash Code:

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

Main Code:

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

    }

What's the issue?? I can understand the code is saying there is nil returning, so what should I put instead of summary??

Cheers, SebOH


Solution

  • Your problem lies in your didStartElement method. When you detect an item you need to initialise summary to an empty NSString @"" - otherwise it will be nil and you will keep calling appendString on nil resulting in a nil value.

    It should be

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