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

  • SEARCH
  • Home
  • 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 8086539
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T18:29:16+00:00 2026-06-05T18:29:16+00:00

I am having a hard time trying to create and populate on the fly

  • 0

I am having a hard time trying to create and populate on the fly an NSMutableDictionary from a tree like structure.

Let’s say you have a node where

node.attributes retrieves an NSArray of key/value pairs

and

node.children retrieves an NSArray of nodes from the same node type

how can you convert that tree into a nested NSMutableDictionary?

my aproach is to try to create a NSMutableDictionary for each node and populate it with its attributes and children, creating a new NSMutableDictionary per child and iterate again with it… it sounds like recursion, isn’t it

The following code works, for one level deep (parent and children) but throw SIGABRT for grandchildren and beyond.

[self parseElement:doc.rootElement svgObject:&svgData];

where

-(void) parseElement:(GDataXMLElement*)parent svgObject:(NSMutableDictionary**)svgObject
{
    NSLog(@"%@", parent.name);

    for (GDataXMLNode* attribute in parent.attributes)
    {
        [*svgObject setObject:attribute.stringValue forKey:attribute.name];
        NSLog(@"  %@ %@", attribute.name, attribute.stringValue);
    }

    NSLog(@"  children %d", parent.childCount);
    for (GDataXMLElement *child in parent.children) {
        NSLog(@"%@", child.name);

        NSMutableDictionary* element = [[[NSMutableDictionary alloc] initWithCapacity:0] retain];

        NSString* key = [child attributeForName:@"id"].stringValue;

        [*svgObject setObject:element forKey:key];
        [self parseElement:child svgObject:&element];
    }
}

UPDATE:

thanks for your answers, I managed to do the code to work

apparently GDataXMLElement doesn’t respond to attributeForName when there is no atributes and so my code threw some exeptions, that where dificult to debug being a recursive method

I am taking into account all your (best practice related) sugestions too

Regards

  • 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-06-05T18:29:17+00:00Added an answer on June 5, 2026 at 6:29 pm

    Please note that I replaced your double indirections by a simple pointer. The only case I know where pointers to pointers make sense is in the connection with NSError. I would rewrite this part of code:

    -(void) parseElement:(GDataXMLElement*)parent svgObject:(NSMutableDictionary*)svgObject
    {
    
    for (GDataXMLNode* attribute in parent.attributes)
    {
        // setObject:forKey: retains the object. So we are sure it won't go away.
        [svgObject setObject:attribute.stringValue forKey:attribute.name];
    }
    
    
    for (GDataXMLElement *child in parent.children) {
        NSLog(@"%@", child.name);
        // Here you claim ownership with alloc, so you have to send it a balancing autorelease.
        NSMutableDictionary* element = [[[NSMutableDictionary alloc] init] autorelease];
    
        // You could also write [NSMutableDictionary dictionary];
    
        NSString* key = [child attributeForName:@"id"].stringValue;
    
        // Here your element is retained (implicitly again) so that it won't die until you let it.
        [svgObject setObject:element forKey:key];
        [self parseElement:child svgObject:element];
    }
    

    }

    If you don’t trust in the magic behind implicit retains, just read what Apple tells you about the setObject:forKey:

    • (void)setObject:(id)anObject forKey:(id)aKey Parameters

    anObject

    The value for key. The object receives a retain message before being added to the dictionary. This value must not be nil.
    

    EDIT: Forgot about your first part:

    NSMutableDictionary* svgData = [[NSMutableDictionary dictionary];
    [self parseElement:doc.rootElement svgObject:svgData];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having a hard time trying to create a XmlRepository. The problem here I
I'm having a hard time with the XSD files. I'm trying to create an
I'm having a hard time trying to display how my textblock look like in
I am having hard time with php headers, i'm trying to create any kind
I'm having a pretty hard time trying to create a write-once field in a
I'm having a hard time trying to create the query. This is the file:
I'm having hard time trying to figure out how to auto-save user data in
I'm having hard time trying to force https (ssl) to particular page on codeiginiter
I'm having a hard time trying to use .strip with the following line of
I'm having a hard time trying to get hasMany to work in Grails 2.0.1

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.