0

Facing issue with NSMutableDictionary,

I have one main mutable dictionary, which is dictMain,

Taken another mutable dictionary, which is dictSubMain,

Declaration as below in ViewController.m file,

@interface ViewController ()
{
    NSMutableDictionary *dictMain;
    NSMutableDictionary *dictSubMain;
}

Now I have some values filled in dictMain initially from viewDidLoad method.

I have one another method, in which very firstly I am assigning dictMain to another mutable dictionary dictSubMain then I made some changes in dictSubMain but the issue is whatever changes I am making in dictSubMain that same changes reflecting on dictMain also. I don't want to make any changes in dictMain.

Below is my code,

-(void)addRowsInTable:(NSInteger)index
{
    dictSubMain = [[NSMutableDictionary alloc] initWithDictionary:dictMain];

     NSMutableArray *tmpArray = [[NSMutableArray alloc] init];
     NSMutableDictionary *tmpDic = [[NSMutableDictionary alloc] init];

    [tmpDic setObject:self.btnTTNumDent.currentTitle forKey:kNUMDENT_T];
    [tmpDic setObject:self.btnTTSizeDent.currentTitle forKey:kSIZEDENT_T];
    [tmpArray addObject:tmpDic];
    [[[dictSubMain valueForKey:kDATA] objectAtIndex:index] setObject:tmpArray forKey:kTABLE];
}

Can anyone help me out to resolve it! Thanks in advance.

iGatiTech
  • 2,306
  • 1
  • 21
  • 45
  • Stop confusing yourself and everyone else. Declare properties. And make instance variables start with an underscore. Since properties, instance variables, global variables, parameters, local variables all look the same in your code, you _will_ have hard to find bugs. And don't use valueForKey. Read it's documentation and use it if that's what you want to use, but not for no reason. – gnasher729 Aug 08 '16 at 07:45

2 Answers2

1

It looks like dictSubMain[kDATA] is an array of mutable dictionaries. You are probably having a problem because [[NSMutableDictionary alloc] initWithDictionary:dictMain] creates a shallow copy of dictMain, so dictSubMain contains the same instance of the array at kDATA as dictMain.

You need to perform a deep copy instead. Methods for doing so are easily searched for. I am sure there are many answers available on SO.

Avi
  • 7,469
  • 2
  • 21
  • 22
0

Try this:

@property(nonatomic, copy) NSMutableDictionary *dictMain;
@property(nonatomic, copy) NSMutableDictionary *dictSubMain;

Actually actually whenever you use NSMutable* style use copy keyword, that's why it exists.

Or change:

dictSubMain = [[NSMutableDictionary alloc] initWithDictionary:dictMain];

to:

dictSubMain = [[NSMutableDictionary alloc] initWithDictionary:dictMain copyItems:YES];
William Hu
  • 15,423
  • 11
  • 100
  • 121
  • Those are functionally identical, and your answer will not solve his issue. – Avi Aug 08 '16 at 07:26
  • Try to add copy property, actually Whenever you use `NSMutable` style use `copy` keyword. – William Hu Aug 08 '16 at 07:30
  • @Gati You don't have to use self execute deep copy way that's theory. Check my edit. Long time didn't use Objc, *deep copy* was in api method already. – William Hu Aug 08 '16 at 07:40