I’m working on my first somewhat serious iOS app and I’m new to Xcode, iOS, and Objective-C. I’ve followed along with, and done a the first few assignments from Stanford’s iOS class on iTunesU. Aside from that I have no experience.
Anyways, I’m using an NSXMLParser to parse an XML file retrieved from the internet, and then I’m trying to create UGStory objects to represent some of the data and add them to an NSMutableArray.
EDIT: I’ve made some changes to the code and updated it here. Now I am sure there is a problem with my NSXMLParser functions. Now when the program executes I end up with 30 objects in the array, but when I inspect the array every object says “Out of Scope” and “Summary Unavailable”.
Also, this data is being used to populate a split view controller. Titles go in the masterview, links are used to fill the detailview. But after this executes there is only one thing in the master; “News @ Ultimate-Guitar.Com”
That is the first text enclosed by headers and at this point the header is yet to be encountered. Because of this I should have no UGStory objects so I’m confused by how this cell is being set. Also everything in the array is “Out of Scope” so where is the controller getting this data from? The controller does nothing more than create a UGFeedReader and send it the parseFeed message.
#import "UGFeedReader.h"
@implementation UGFeedReader
@synthesize currentTag = _currentTag;
@synthesize stories = _stories;
@synthesize read = _read;
@synthesize i = _i;
- (void)parseFeed
{
NSURL* ugFeed = [NSURL URLWithString:@"http://www.ultimate-guitar.com/modules/rss/news.xml.php"];
NSXMLParser* feedParser = [[NSXMLParser alloc]initWithContentsOfURL:ugFeed];
_stories = [[NSMutableArray alloc] initWithCapacity:10];
_i = 0;
_read = NO;
[feedParser setDelegate:self];
[feedParser parse];
}
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict {
if ([elementName isEqualToString:@"item"])
{
_read = YES;
UGStory* temp = [[UGStory alloc] init];
[_stories addObject:temp];
}
return;
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
if (!_currentTag)
{
_currentTag = [[NSMutableString alloc] init];
}
[_currentTag appendString:string];
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
if (_read == YES)
{
if ([elementName isEqualToString:@"title"]) {
UGStory* temp = [_stories objectAtIndex:_i];
temp.title = _currentTag;
}
if ([elementName isEqualToString:@"link"]) {
UGStory* temp = [_stories objectAtIndex:_i];
temp.url = [NSURL URLWithString:_currentTag];
_read = NO;
_i++;
}
}
return;
}
@end
On this line:
you probably meant:
On a
nilarray all theaddObject:messages are ignored.You’re never
nil‘ing_currentTagafter you end an element.Add this code to the end of your
didEndElement:and it should work as you expected.