Can't get the links set


I’m working through the last part of the chapter when we create an RSSItem and record the Title and Link of the items in RSSItem.m

The strange thing is everything seems to work fine, but when I click on one of the cells, the browser pulls up an empty window. So, I started troubleshooting. Basically, I started adding some NSLog statements to see what was being recorded. It seems that the title part is working fine, but the links are the actual problem. Here is the code:

- (void)parser:(NSXMLParser *)parser 
   didStartElement:(NSString *)elementName 
      namespaceURI:(NSString *)namespaceURI 
     qualifiedName:(NSString *)qName 
        attributes:(NSDictionary *)attributeDict
   NSLog(@"\t\t%@ found a %@ element", self, elementName);
   if ([elementName isEqual:@"title"]) {
      NSLog(@"Start Title");                                             // **** Added
      currentString = [[NSMutableString alloc] init];
      [self setTitle:currentString];
      NSLog(@"Title Set");                                             // **** Added
   else if ([elementName isEqual:@"link"]) {
      NSLog(@"Start Link");                                             // **** Added
      currentString = [[NSMutableString alloc] init];
      [self setLink:currentString];
      NSLog(@"Link set");                             // ****** Added

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)str
   [currentString appendString:str];
   NSLog(@"Found Characters:  %@", str);                      // ******** Added

This is the Log of one XML Item:

2012-06-11 19:03:05.115 Nerdfeed[1479:f803] 	<RSSChannel: 0x68b3f40> found a item element
2012-06-11 19:03:05.115 Nerdfeed[1479:f803] Found Characters:  
2012-06-11 19:03:05.116 Nerdfeed[1479:f803] 		<RSSItem: 0x6ac0580> found a title element
2012-06-11 19:03:05.116 Nerdfeed[1479:f803] Start Title
2012-06-11 19:03:05.117 Nerdfeed[1479:f803] Title Set
2012-06-11 19:03:05.118 Nerdfeed[1479:f803] Found Characters:  32. Blocks :: ARC issue with devowelizer :: Author chrisleewashere
2012-06-11 19:03:05.118 Nerdfeed[1479:f803] 	<RSSChannel: 0x68b3f40> found a link element
2012-06-11 19:03:05.118 Nerdfeed[1479:f803] 	<RSSChannel: 0x68b3f40> found a description element
2012-06-11 19:03:05.119 Nerdfeed[1479:f803] 	<RSSChannel: 0x68b3f40> found a author element
2012-06-11 19:03:05.120 Nerdfeed[1479:f803] 	<RSSChannel: 0x68b3f40> found a category element
2012-06-11 19:03:05.123 Nerdfeed[1479:f803] 	<RSSChannel: 0x68b3f40> found a comments element
2012-06-11 19:03:05.137 Nerdfeed[1479:f803] 	<RSSChannel: 0x68b3f40> found a pubDate element
2012-06-11 19:03:05.137 Nerdfeed[1479:f803] 	<RSSChannel: 0x68b3f40> found a guid element

What I don’t understand is why the log statements aren’t being recorded for the Link element. The didStartElement method is logging that it found a link element, but then why isn’t the else if statement firing off the “Start Link” and “Link Set” messages?

Somewhere, I had a NSLog statement print out the contents of [entry link], which was null. This is what started my search for the problem where is was being set. Obviously [self setLink:currentString] isn’t getting processed, since the else if statement isn’t being recognized as True.


Ah Hah!

Found it. In the code for parser:didEndElement I was setting the parentParserDelegate back at the end of the title element instead of at the end of the item element. So, RSSItem was no longer the delegate of the parser by the time the link element was found. The entire parser:didStartElement method wasn’t even being fired within the RSSItem any longer

- (void)parser:(NSXMLParser *)parser 
 didEndElement:(NSString *)elementName 
  namespaceURI:(NSString *)namespaceURI 
 qualifiedName:(NSString *)qName
   currentString = nil;
   if ([elementName isEqual:@"item"]) {                     //  <--  I had set this to "title" instead of "item" as it's now shown
      [parser setDelegate:parentParserDelegate];

Always feels good to find your error when you’re first starting out!