Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><strong>IMPORTANT</strong>: The question (and my code below) both deal with a very specific case, in which the NSMutableDictionary contains <em>only</em> arrays of strings. These solutions <em>will not work</em> for more complex examples. For more general case solutions, see the following:</p> <ul> <li><a href="https://stackoverflow.com/a/1950462/33686">Tom Dalling's answer</a></li> <li><a href="https://stackoverflow.com/a/1950456/33686">dreamlax's answer</a></li> <li><a href="https://gist.github.com/yfujiki/1664847" rel="nofollow noreferrer">Source from yfujiki on GitHub Gist</a></li> </ul> <hr> <p><strong>Answer for this specific case:</strong></p> <p>Your code should work, but you will definitely need the <code>[oneCopy release]</code>. The new dictionary will retain the copied objects when you add them with <code>setValue:forKey</code>, so if you do not call <code>[oneCopy release]</code>, all of those objects will be retained twice.</p> <p>A good rule of thumb: if you <code>alloc</code>, <code>retain</code> or <code>copy</code> something, you must also <code>release</code> it.</p> <p>Note: here is some sample code that would work <em>for certain cases only</em>. This works because your NSMutableDictionary contains only arrays of strings (no further deep copying required):</p> <pre><code>- (NSMutableDictionary *)mutableDeepCopy { NSMutableDictionary * ret = [[NSMutableDictionary alloc] initWithCapacity:[self count]]; NSMutableArray * array; for (id key in [self allKeys]) { array = [(NSArray *)[self objectForKey:key] mutableCopy]; [ret setValue:array forKey:key]; [array release]; } return ret; } </code></pre>
 

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