Trouble updating nested NSMutableDictionary

I am having trouble updating objects within a nested structure. I have an

NSMutableDictionary 

that contains multiple nsmutableDictionaries and in another case there is an additional level of nsmutableDictionaries for each of those in the previous level.

These are mutable types so I assume simply setObject:ForKey: will work fine. For some reason, when the structure is nested, any attempt to update a single inner level dictionary, it overrides all objects values for ever dictionary at this level.

I have tried a number of things from parsing out the values and attempting to reinsert, to removing the object for key, but nothing seems to work.

Any help would be appreciated.

Here is my code: I have a textfield and segmentedControl setup up with a few index's to demo and whichever is selected is the index it uses to save/update the object to. I am using NSNumber's for the keys.

Update:

//viewDidLoad

self.content = [NSMutableDictionary new];
self.contentContainer = [NSMutableDictionary new];


- (IBAction)like_unlike:(id)sender {
  BOOL isLiked = [self.textField.text boolValue];

  NSLog(@"isLiked - %d", isLiked ? YES : NO);

  NSNumber *nsIsLiked = [NSNumber numberWithBool:isLiked];

  NSLog(@"nsIsLiked - %@", nsIsLiked);


  [self.content setObject:nsIsLiked forKey:@"updatedIsLiked"];


  [self.contentContainer setObject:self.content forKey:[NSNumber numberWithInteger:self.segment.selectedSegmentIndex]];

}

NSLog(@"COMPLETE DIC - %@", self.contentContainer);

}

This is the output. I save 3 values. index 0, 2 and 4.

2014-11-08 11:45:41.855 DictionariesNested[1039:125297] isLiked - 1
2014-11-08 11:45:41.855 DictionariesNested[1039:125297] nsIsLiked - 1
2014-11-08 11:45:41.855 DictionariesNested[1039:125297] COMPLETE DIC - {
0 =     {
    updatedIsLiked = 1;
};
}
2014-11-08 11:45:43.623 DictionariesNested[1039:125297] isLiked - 1
2014-11-08 11:45:43.623 DictionariesNested[1039:125297] nsIsLiked - 1
2014-11-08 11:45:43.623 DictionariesNested[1039:125297] COMPLETE DIC - {
0 =     {
    updatedIsLiked = 1;
};
2 =     {
    updatedIsLiked = 1;
};
}
2014-11-08 11:45:45.018 DictionariesNested[1039:125297] isLiked - 1
2014-11-08 11:45:45.018 DictionariesNested[1039:125297] nsIsLiked - 1
2014-11-08 11:45:45.018 DictionariesNested[1039:125297] COMPLETE DIC - {
0 =     {
    updatedIsLiked = 1;
};
4 =     {
    updatedIsLiked = 1;
};
2 =     {
    updatedIsLiked = 1;
};
}

Then upon updating index 0 to false, all values are changed to 0.

2014-11-08 11:48:01.161 DictionariesNested[1188:161731] isLiked - 0
2014-11-08 11:48:01.161 DictionariesNested[1188:161731] nsIsLiked - 0
2014-11-08 11:48:01.162 DictionariesNested[1188:161731] COMPLETE DIC - {
0 =     {
    updatedIsLiked = 0;
};
4 =     {
    updatedIsLiked = 0;
};
2 =     {
    updatedIsLiked = 0;
};
}

Edit: I rewrite my answer since you update your question

When you are doing self.contentContainer setObject: , you are storing each time the same dictionary, not a specific one for the selected segment. So, if you change one, all will change because it's the same instance. In order to have one dictionary for each segment, you have to instantiate a new one if it doesn't already exist.

In order to solve your issue, you have to instantiate one NSMutableDictionary for each segment. Something like that should work:

NSMutableDictionary *content = [self.contentContainer objectForKey:self.segment.selectedSegmentIndex];
if (!content) {
    content = [[NSMutableDictionary alloc] init];
    [content setObject:nsIsLiked forKey:@"updatedIsLiked"];
}
[self.contentContainer setObject:content forKey:[NSNumber numberWithInteger:self.segment.selectedSegmentIndex]];
链接地址: http://www.djcxy.com/p/47348.html

上一篇: IOS KVO工作错了

下一篇: 无法更新嵌套的NSMutableDictionary