Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Basically, the problem is that you are setting the level dictionary for each level to the same stats object:</p> <pre><code> for (int i = 1; i&lt;=18; i++) { [levels setObject:stats forKey:[NSString stringWithFormat:@"level%d", i]]; } </code></pre> <p>Instead, you need to set them each to a different mutable copy of the object:</p> <pre><code> for (int i = 1; i&lt;=18; i++) { NSMutableDictionary *statsCopy = [stats mutableCopy]; [levels setObject:statsCopy forKey:[NSString stringWithFormat:@"level%d", i]]; [statsCopy release]; } </code></pre> <p>That should solve the first part for you. However, you also have the same problem with the levelSets. Basically, whenever you add a mutable dictionary, you need to determine whether you are trying to point at the same mutable object (you want to keep them synchronized), or whether you want copies of that mutable object (thus, they act independently). And when you look at building this kind of tree structure, that means you need to look at this at every level.</p> <p>So, looking at the rest of your code, you'll also need to fix your level sets and categories in the same way, by making a mutable copy inside of each loop and then adding that mutable copy instead of adding the original object. And, since you want to mutate the contents of the contents of the dictionaries, every time you make a mutable copy that contains another mutable dictionary, you need to make that copy at each depth.</p> <p>You are going to have to choose between building these things depth-wise or doing a deep copy. There's another question: <a href="https://stackoverflow.com/questions/1950361/deep-mutable-copy-of-a-nsmutabledictionary">deep mutable copy of a NSMutableDictionary</a> That covers deep copies of mutable dictionaries pretty well. Alternatively, you can invert the building of the structure so that instead of making copies, you build each dictionary except for <code>stats</code> which you can easily copy.</p> <p>The deep copy creates copies of each of the mutable entries all the way down to the farthest leaf nodes. If you think about the NSMutableDictionaries as a tree with the base of the tree at the top ( completedLevels in your case) and the leaves as the elements of the stats NSMutableDictionaries that you copy, you want to individually manipulate every element of the tree, whether it is the leaf node or any intermediate. </p> <p>An illustration could be made as follows, with this representing the contents of <code>MyDictionary</code>:</p> <pre><code> Top-A: Second-A-1 Second-A-2 Top-B: Second-B-1 Second-B-2 </code></pre> <p>To recap, you have the <code>MyDictionary</code> at the top, which has 2 keys (<code>Top-A</code>,<code>Top-B</code>), each of which is associated with a separate NSMutableDictionary object. Each of those NSMutableDictionaries has 2 keys, each associated with a separate object.</p> <p>When you make a <code>-mutableCopy</code> of <code>MyDictionary</code>, the result is a new <code>NSMutableDictionary</code> with 2 keys (<code>Top-A</code>,<code>Top-B</code>), each associated with an NSMutableDictionary. <strong>However</strong>, these <code>NSMutableDictionary</code> objects are actually the same objects as the ones in <code>MyDictionary</code>. This is a shallow copy, meaning that there is a new top-level object (the mutable copy of <code>MyDictionary</code>), but that objects associated with each of the keys in the new dictionary are the same objects as were associated with the keys in <code>MyDictionary</code>. As such, if you change <code>Top-A</code> in the copy be associated with a different <code>NSMutableDictionary</code>, then the <code>Top-A</code> in <code>MyDictionary</code> and the copy will no longer be the same. But, if you change the value associated with <code>Second-A-1</code>, it will change both in <code>MyDictionary</code> and the copy of <code>MyDictionary</code> because <code>Top-A</code> points at a single object.</p> <p>When you make a deep copy, the utility copies every element of every dictionary individually, which means that the copy of <code>MyDictionary</code> will have a separate <code>Top-A</code>, <code>Second-A-1</code>, <code>Second-A-2</code>,<code>Top-B</code>, etc. from the ones that exist in <code>MyDictionary</code>, and therefore, you can change the values of any of the dictionaries (no matter how deep) without fear of changing other objects.</p>
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. 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