ios - Parsing summary from RSS Feed causes app to crash -
i trying add summary uitableview parsing rss feed. have set method summary, app crashing. victim line is:
[item setobject:summary forkey:@"summary"];
crash code:
2014-05-17 09:32:09.231 ***[1169:90b] *** terminating app due uncaught exception 'nsinvalidargumentexception', reason: '*** setobjectforkey: object cannot 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 issue?? can understand code saying there nil returning, should put instead of summary??
cheers, seboh
your problem lies in didstartelement
method. when detect item
need initialise summary empty nsstring @"" - otherwise nil , keep calling appendstring
on nil resulting in nil value.
it should
- (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]; } }
Comments
Post a Comment