Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3346194
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T01:16:29+00:00 2026-05-18T01:16:29+00:00

I’ve read posts about this, and it seems pretty straight-forward. I’m pretty new to

  • 0

I’ve read posts about this, and it seems pretty straight-forward. I’m pretty new to Obj-C and iPhone dev in general, so I could easily be overlooking something. I can’t seem to return the NSMutableArray with the Article objects. I don’t get any errors, but when I try to NSLog() some stuff I’m getting EXEC_BAD_ACCESS errors (I’m assuming a memory access issue?). I have an ArticlesParser class that does the parsing… Here’s what it looks like:

// ArticlesParser.h
#import <Foundation/Foundation.h>
#import "Article.h"

@class Article;

@interface ArticlesParser : NSObject <NSXMLParserDelegate> {
 NSMutableString *currentCharaters;
 Article *currentArticle; 
 NSMutableArray *articlesCollection; 
 NSMutableData *xmlData;
 NSURLConnection *connectionInProgress;
 BOOL connectionHasCompleted;
}

@property (nonatomic, assign) BOOL connectionHasCompleted;

- (void)parseUrl:(NSString *)url;
- (void)beginParsing:(NSURL *)xmlUrl;
- (NSMutableArray *)arrayOfArticles;

@end

Here’s the implementation…

// ArticlesParser.m
#import "ArticlesParser.h"

@implementation ArticlesParser

@synthesize connectionHasCompleted;

#pragma mark -
#pragma mark Parsing methods

- (void)parseUrl:(NSString *)url
{
 [self setConnectionHasCompleted:NO];
 NSURL *xmlUrl = [NSURL URLWithString:url];
 [self beginParsing:xmlUrl];
}

- (void)beginParsing:(NSURL *)xmlUrl
{
 [articlesCollection removeAllObjects];
 articlesCollection = [[NSMutableArray alloc] init];

 NSURLRequest *request = [NSURLRequest requestWithURL:xmlUrl cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:30];

 // clear existing connection if there is one
 if (connectionInProgress) {
  [connectionInProgress cancel];
  [connectionInProgress release];
 }

 [xmlData release];
 xmlData = [[NSMutableData alloc] init];

 // asynchronous connection
 connectionInProgress = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES];
}

- (NSMutableArray *)arrayOfArticles
{
 // NOT RETURNING ANYTHING
 return articlesCollection;
}

#pragma mark -
#pragma mark NSXMLParserDelegate methods

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
 [xmlData appendData:data];
}

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
 if ([elementName isEqual:@"article"]) {
  currentArticle = [[Article alloc] init];
  return;
 }
 if ([elementName isEqual:@"title"]) {
  currentCharaters = [[NSMutableString alloc] init];
  return;
 }
 if ([elementName isEqual:@"last_updated"]) {
  currentCharaters = [[NSMutableString alloc] init];
  return;
 }
}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
 [currentCharaters appendString:string];
}

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
 if ([elementName isEqual:@"article"]) {
  [articlesCollection addObject:currentArticle];
  [currentArticle release], currentArticle = nil;
  return;
 }
 if ([elementName isEqual:@"title"]) {
  [currentArticle setTitle:currentCharaters];
  [currentCharaters release], currentCharaters = nil;
  return;
 }
 if ([elementName isEqual:@"last_updated"]) {
  [currentArticle setLastModified:currentCharaters];
  [currentCharaters release], currentCharaters = nil;
  return;
 }
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
 NSXMLParser *parser = [[NSXMLParser alloc] initWithData:xmlData];
 [parser setDelegate:self];

 [parser parse];
 [parser release];

 [self setConnectionHasCompleted:YES];
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
 [currentArticle release];
 currentArticle = nil;

 [currentCharaters release];
 currentCharaters = nil;

 [articlesCollection release];
 articlesCollection = nil;

 [connectionInProgress release];
 connectionInProgress = nil;

 [xmlData release];
 xmlData = nil;

 NSLog(@"connection failed: %@", [error localizedDescription]);
}

@end

I know that the actual parsing works because I did have this directly in my view controller and everything worked fine. But now I want to access basically the same thing from another controller, only the URL is different (returns the same formatted XML though).

Here’s how I’m trying to make use of this class in my controller:

// instance method called within an articles controller
// that is to load the results in a table view
- (void)loadArticles
{
    // (leaving off the URL because it's not important)
 NSString *urlToRequest = [NSString stringWithFormat:@"...", [self letterToList]];
 ArticlesParser *aParser = [[ArticlesParser alloc] init];

    // initiate the parsing
 [aParser parseUrl:urlToRequest];

    // load up the articles ivar so the tableview can
    // make use of it to load its cells
 articles = [aParser arrayOfArticles];
}

Is there something obvious that I’m missing? Is this even a good way to share the NSXMLParser code?

I’m pulling my hair out over this one… thanks in advance!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T01:16:30+00:00Added an answer on May 18, 2026 at 1:16 am

    What is it you’re trying to NSLog that generates the EXEC_BAD_ACCESS error? Looking at your code your call to arrayOfArticles should return an NSMutableArray with no elements, so e.g. something like this would understandably give an EXEC_BAD_ACCESS:

    NSLog(@"%@", [[articles objectAtIndex:0] description]); // index out of bounds
    

    By having your XML parser class also responsible for fetching the data it’s going to parse (using NSURLConnection) you’ve made it asynchronous, which means it’s no longer suitable to be used like this:

    ArticlesParser *ap = [[[ArticlesParser alloc] init] autorelease];
    [ap parseURL:@"http://example.com/foo"];
    NSArray *anArray = [ap arrayOfArticles];
    

    anArray is now an empty array, and will only be populated at some indeterminate point in the future, if at all – and you can’t detect when that time comes without polling the array. Urgh! 🙂

    There are a couple of ways you might get around this. One approach is to have your XML Parser class declare delegate methods, offering callbacks for when the XML data has been fully fetched and parsed and when error conditions occur (in much the same way that the delegate methods in NSURLConnection work). Another approach is to have your XML Parser class be a simple (synchronous) XML parser, and move the asynchronous data-fetching code to outside your class.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
This could be a duplicate question, but I have no idea what search terms
I'm not entirely sure how I managed to jack this up. http://pretty-senshi.com If you
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I want use html5's new tag to play a wav file (currently only supported

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.