Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><code>init...</code> methods should only be called once, immediately after a call to <code>alloc</code> or <code>allocWithZone:</code>. When framework code creates and returns an object or graph of objects, their <code>init...</code> methods have already been called, so sending another <code>init...</code> message would have undefined results. Don't do that.</p> <p>Interestingly, in spite of what the documentation appears to say (and admittedly I probably missed a key sentence or paragraph somewhere), when you create an instance of a mutable collection by reading a plist, any nested collections are also mutable. I ran the following little experiment in a test harness just to be sure:</p> <pre><code>NSMutableDictionary *pets = [NSMutableDictionary dictionaryWithContentsOfFile:@"/tmp/Pets.plist"]; NSMutableArray *cats = [pets objectForKey:@"cats"]; [cats addObject:@"Foo"]; // EDIT: Added line I accidentally omitted earlier NSLog(@"%@", cats); </code></pre> <p>So again, the nested collections created when you read in the plist are fully initialized, and mutable to boot, so you can simply use them, as you've been doing.</p> <p><strong>EDIT</strong></p> <p>However, after doing some further reading of the docs, I think the OP is right to feel uneasy about relying on what is apparently an undocumented feature of the current version of the SDK. For example, the <em>Property List Programming Guide</em> states:</p> <blockquote> <p>If you load the property list with this call:</p> <pre><code>NSMutableArray * ma = [NSMutableArray arrayWithContentsOfFile:xmlFile]; </code></pre> <p><code>ma</code> is a mutable array with immutable dictionaries in each element. Each key and each value in each dictionary are immutable.</p> </blockquote> <p>So, to be on the safe side, if you need a nested collection to be mutable, you should create it yourself. For example, I'd recommend rewriting the code in the example above as follows:</p> <pre><code>NSMutableDictionary *pets = [NSMutableDictionary dictionaryWithContentsOfFile:@"/tmp/Pets.plist"]; NSArray *cats = [pets objectForKey:@"cats"]; NSMutableArray *mutableCats = [cats mutableCopy]; [pets setObject:mutableCats forKey:cats]; [mutableCats release]; </code></pre> <p>You can then safely make changes to the nested mutable collection:</p> <pre><code>[mutableCats addObject:@"Foo"]; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

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