Note that there are some explanatory texts on larger screens.

plurals
  1. PODo I need to initialize an iOS empty nested array that's part of a plist import?
    text
    copied!<p>the code below is working, but I want to make sure it's <em>correct</em>. 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, <code>initWithCapacity:1</code> then you often get memory errors once you start trying to add items.</p> <p>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?</p> <p>Any and all comments appreciated. Thanks.</p> <p>First, here's what the plist looks like that I'm using to create my dictionary:</p> <p><img src="https://i.stack.imgur.com/RGLdH.png" alt="someData.plist"></p> <p>Next, here's my code where I'm using the plist to create a dictionary, then adding an item to <code>dataArray</code></p> <p><code></p> <pre><code>// 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); </code></pre> <p></code></p> <p>Okay, fine, simple, good. When I Log the dictionary contents it shows that "23" has been added as an array value to <code>dataArray</code>. 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.</p> <p>So to sum up, <code>dataArray</code> is an empty array inside the .plist, so do I need to initialize it somehow (using, for example <code>initWithCapacity:</code> or something else) before I can properly populate it, or is the way I'm coding here just fine?</p> <p>Thanks again.</p> <p><strong>EDIT</strong></p> <p>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 <a href="http://www.mlsite.net/blog/?p=895" rel="nofollow noreferrer" title="this link on deep copying">this link on deep copying</a>. His <a href="http://www.mlsite.net/blog/?p=213" rel="nofollow noreferrer" title="previous posts on deep copying">previous posts on deep copying</a> 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.</p> <p>However, as mentioned in the link above, it looks like these methods were superfluous, due to the <a href="http://developer.apple.com/library/ios/#documentation/CoreFoundation/Reference/CFPropertyListRef/Reference/reference.html" rel="nofollow noreferrer"><code>CFPropertyListCreateDeepCopy</code></a> method, which can be invoked with a call such as</p> <pre><code>testData = CFPropertyListCreateDeepCopy(kCFAllocatorDefault, [NSDictionary dictionaryWithContentsOfFile:path], kCFPropertyListMutableContainersAndLeaves); </code></pre> <p>So, my question is, can I properly use <code>CFPropertyListCreateDeepCopy</code>, 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?</p> <p>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.</p> <p>(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 <em>correct</em>)</p> <p>Thanks in advance.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload