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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T05:31:30+00:00 2026-05-21T05:31:30+00:00

the code below is working, but I want to make sure it’s correct .

  • 0

the code below is working, but I want to make sure it’s correct. I’m nervous about having an empty Array inside my dictionary that I create from the plist, since typically it seems that if you don’t, say, initWithCapacity:1 then you often get memory errors once you start trying to add items.

At least, that’s been my experience with NSMutableDictionary. However, this is the first time I’m trying to implement nested data objects, so perhaps the reason this code works is that the nested array is automatically initialized when it’s imported as part of its parent dictionary?

Any and all comments appreciated. Thanks.

First, here’s what the plist looks like that I’m using to create my dictionary:

someData.plist

Next, here’s my code where I’m using the plist to create a dictionary, then adding an item to dataArray

// Create a pointer to a dictionary

NSMutableDictionary *dictionary;

// Read "SomeData.plist" from application bundle

NSString *path = [[NSBundle mainBundle] bundlePath];
NSString *finalPath = [path stringByAppendingPathComponent:@"SomeData.plist"];
dictionary = [NSMutableDictionary dictionaryWithContentsOfFile:finalPath];


// Now let's see if we can successfully add an item to the end of this empty nested array. How 'bout the number 23

NSNumber *yetAnotherNumber = [NSNumber numberWithInt:23];

[[dictionary objectForKey:@"dataArray"] addObject:yetAnotherNumber];

// Dump the contents of the dictionary to the console

NSLog(@"%@", dictionary);

Okay, fine, simple, good. When I Log the dictionary contents it shows that “23” has been added as an array value to dataArray. So the code works. But again, I want to confirm that I’m not “getting lucky” here, with my code just happening to work even though I’m not properly initializing that nested array. If so, then I could run into unanticipated errors later on.

So to sum up, dataArray is an empty array inside the .plist, so do I need to initialize it somehow (using, for example initWithCapacity: or something else) before I can properly populate it, or is the way I’m coding here just fine?

Thanks again.

EDIT

Hey all. I’ve been doing continued research on this, in the interests of finding a satisfying answer. I think I may have stumbled upon something, via this link on deep copying. His previous posts on deep copying had presented some code to do essentially what I was looking for above: create a mutable copy of a dictionary or array, from a plist, that also has mutable sub-structures.

However, as mentioned in the link above, it looks like these methods were superfluous, due to the CFPropertyListCreateDeepCopy method, which can be invoked with a call such as

testData = CFPropertyListCreateDeepCopy(kCFAllocatorDefault, [NSDictionary dictionaryWithContentsOfFile:path], kCFPropertyListMutableContainersAndLeaves);

So, my question is, can I properly use CFPropertyListCreateDeepCopy, in the way shown, to achieve what I’ve been asking about here? In other words, can I use this method to import my dictionary from a plist with fully mutable, nested data objects?

As I mentioned in one of the comments, I know I can create a nested, mutable dictionary manually, but for complex data that’s just not practical, and it seems unlikely that built-in methods to import a mutable plist don’t exist. So, based on the above, it looks like I’ve possibly found the solution, but I’m still too new to this to be able to say for sure. Please advise.

(Side note: I would simply test the code, but as we’ve established, the current SDK is buggy with regard to allow you to edit immutable nested dictionaries, contrary to the documented behavior. So as before, I’m not just interested in whether this works, but whether it’s correct)

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-21T05:31:31+00:00Added an answer on May 21, 2026 at 5:31 am

    Any object in a dictionary which is created by reading from disk will be properly initialized. You will not have to do it on your own. However, as pointed out by jlehr, contents of the dictionary should be immutable. If you want the contents of the dictionary to be mutable, you will need to change them on your own. I have no idea why your program is not throwing an exception.

    I do not know why you are getting memory errors while not using initWithCapacity:1 in other situations. The following code is perfectly valid:

    NSMutableArray *array = [[NSMutableArray alloc] init];
    [array addObject:@"object1"];
    [array addObject:@"object2"];
    NSLog(@"%@",array);
    [array release];
    

    If you don’t specify a capacity, the array won’t pre-allocate any memory, but it will allocate memory as required later.

    Edit:

    It is perfectly acceptable to use NSDictionary with CFPropertyListCreateDeepCopy. In Core Foundation, a CFPropertyList can be a CFDictionary, CFArray, CFNumber, CFString, or CFData. Since NSDictionary is toll-free bridged to CFDictionary, you can use it wherever a CFDictionary is asked for by casting, and vice-versa. Your code as is will give a warning, but you can suppress it by casting the dictionary and return values.

    NSDictionary *testData = (NSDictionary*)CFPropertyListCreateDeepCopy(kCFAllocatorDefault, (CFDictionaryRef)[NSDictionary dictionaryWithContentsOfFile:path], kCFPropertyListMutableContainersAndLeaves);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Code below is not working as expected to detect if it is in design
I am using the code snippet below, however it's not working quite as I
EDIT: See my working code in the answers below. In brief: I have a
Update: Solved, with code I got it working, see my answer below for the
The code below works. But if I comment out the line Dim objRequest As
I'm using $().each() to loop through some items. I want to make sure that
Code below does not run correctly and throws InvalidOperationExcepiton . public void Foo() {
The code below shows a sample that I've used recently to explain the different
The code below gives me this mysterious error, and i cannot fathom it. I
The code below gives an error: Property 'Int32 Key' is not defined for type

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.