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 472971
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T00:08:24+00:00 2026-05-13T00:08:24+00:00

I am trying to build an array that contains arrays of dictionary objects in

  • 0

I am trying to build an array that contains arrays of dictionary objects in Xcode. I can create a working array that has one or more dictionaries in it and then use the addObject: method to add this array as an object in my main array. I cycle around this several times, rebuilding the working array and adding objects to the main array. All good so far, except that on inspecting my main array it contains duplicates of the last dictionaries that the working array was built with. I am assuming this is because when I use the addObject it simply assigns a pointer to the array but does not increase the retain count or create a copy of the working array, hence every time it is rebuilt the main array simply points to the this array.

I guess my question is how do I create a copy of the working array add it to the main array and then rebuild it and add it again? Hope that makes sense and sorry if this appears to be a basic question – it is (I’m new to Xcode) – and any help would be really appreciated.


        [sectionArray release];
        sectionArray  = [[NSMutableArray alloc] init];
        [sectionArray addObject:dict];

This all works, which is great. One thing I don’t have my head around yet is, is there a danger that when I am done with the main array and I do a [mainArray release] too that these child arrays will be left behind in memory or will the release destroy them as well?


I have solved it by re-initialising the inner working array instead of just removing all objects, i.e.,

I did have:

        [sectionArray removeAllObjects];
        [sectionArray addObject:dict];

And changed it to:

        sectionArray  = [[NSMutableArray alloc] init];
        [sectionArray addObject:dict];

I have a feeling though that this is not a good thing to do and I will start to leak memory all over the place.

  • 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-13T00:08:24+00:00Added an answer on May 13, 2026 at 12:08 am

    If all you do with sectionArray is to put it in another array you need to release it after you put it in your mainArray so that only mainArray holds a reference to it:

    sectionArray = [[NSMutableArray alloc] init];
    [sectionArray addObject:dict];
    [mainArray addObject:sectionArray];
    [sectionArray release];
    

    When you later release your mainArray reference it will take care of releasing all it’s children and your sectionArray‘s will be freed. (The same goes for the dicts put in sectionArray).

    Hope this helped.

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

Sidebar

Related Questions

I am trying to build a Hash that has an array as one value;
I'm trying to build an array in javascript that contains a list of ids.
I am trying to build a php function that would return an array with
I am trying to build a function in C/C++ to sort an array and
I'm currently trying to build a personal website to create a presence on the
I am trying to build an Eclipse application that would work with a linux/motif
I'm trying to build a nested array: First, I make a PlayerItems array which
I am trying to create a dynamic table that is being built based on
I'm trying to build an array structured like [ [num, [num, num num]], [num,
I'm trying to build a multidimensional associative array while fetching the results from a

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.