Add NSMutableDictionary into Array for Button Action

71 views Asked by At

I have one NSMutableDictionary:

sampleDict = [NSMutableDictionary new];

[sampleDict setObject:@"FooIndex" forKey:@"Key_1"]; // adds @"Foo"
[sampleDict setObject:@"FooOne" forKey:@"Key_2"]; // adds @"Foo"
[sampleDict setObject:@"FooTwo" forKey:@"Key_3"]; // adds @"Foo"
[sampleDict setObject:@"FoFour" forKey:@"Key_4"]; // adds @"Foo"
[sampleDict setObject:@"FooFivve" forKey:@"Key_5"]; // adds @"Foo"

I want add this dictionary into Array on Button action here code like this,

arraytesting = [NSMutableArray new];

- (IBAction)action:(id)sender {
    [arraytesting addObject:sampleDict];
    NSLog(@"Sample arraytesting>>>>> %@",arraytesting);

}

Finally the output is:

(
        {
        "Key_1" = FooIndex;
        "Key_2" = FooOne;
        "Key_3" = FooTwo;
        "Key_4" = FoFour;
        "Key_5" = FooFivve;
    }
)

But when I need to update the dictionary:

[sampleDict setObject:@"one" forKey:@"Key_1"]; // adds @"Foo"
[sampleDict setObject:@"two" forKey:@"Key_2"]; // adds @"Foo"
[sampleDict setObject:@"three" forKey:@"Key_3"]; // adds @"Foo"
[sampleDict setObject:@"FoFour" forKey:@"Key_4"]; // adds @"Foo"
[sampleDict setObject:@"FooFivve" forKey:@"Key_5"]; // adds @"Foo"

The output looks like:

(
    {
        "Key_1" = FooIndex;
        "Key_2" = FooOne;
        "Key_3" = FooTwo;
        "Key_4" = FoFour;
        "Key_5" = FooFivve;
    },
    {
        "Key_1" = FooIndex;
        "Key_2" = FooOne;
        "Key_3" = FooTwo;
        "Key_4" = FoFour;
        "Key_5" = FooFivve;
    }
)

And I want the output to be like:

(
    {
        "Key_1" = FooIndex;
        "Key_2" = FooOne;
        "Key_3" = FooTwo;
        "Key_4" = FoFour;
        "Key_5" = FooFivve;
    },
    {
        "Key_1" = one;
        "Key_2" = two;
        "Key_3" = three;
        "Key_4" = FoFour;
        "Key_5" = FooFivve;
    }
)
3

There are 3 answers

3
Sivajee Battina On
[sampleDict setObject:@"one" forKey:@"Key_1"]; // adds @"Foo"
[sampleDict setObject:@"two" forKey:@"Key_2"]; // adds @"Foo"
[sampleDict setObject:@"three" forKey:@"Key_3"]; // adds @"Foo"
[sampleDict setObject:@"FoFour" forKey:@"Key_4"]; // adds @"Foo"
[sampleDict setObject:@"FooFivve" forKey:@"Key_5"]; // adds @"Foo"

Here you are using same reference of same sampleDict. Instead of that create a new Object again and add.

smapleDict = [NSMutableArray new]

[sampleDict setObject:@"one" forKey:@"Key_1"]; // adds @"Foo"
    [sampleDict setObject:@"two" forKey:@"Key_2"]; // adds @"Foo"
    [sampleDict setObject:@"three" forKey:@"Key_3"]; // adds @"Foo"
    [sampleDict setObject:@"FoFour" forKey:@"Key_4"]; // adds @"Foo"
    [sampleDict setObject:@"FooFivve" forKey:@"Key_5"]; // adds @"Foo"
1
Srj0x0 On

To array you add reference to object. After that, you edit you dictionary and add it reference again. You can call copy to make new instance of sampleDict. You method must be like this:

- (IBAction)action:(id)sender { [arraytesting addObject:[sampleDict copy]]; NSLog(@"Sample arraytesting>>>>> %@",arraytesting); }
3
Reinier Melian On

Your dictionary sampleDict is an reference so you need to mutable copy of it, if you don't and you modify the original dictionary, this will modify all your dicts on your array, so you need copy, modify and add then to your Array, something like this

#import "ViewController.h"

@interface ViewController ()
@property NSMutableArray * arrayTesting;
@property NSMutableDictionary * sampleDict;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.sampleDict = [NSMutableDictionary new];


    [self.sampleDict setObject:@"FooIndex" forKey:@"Key_1"]; // adds @"Foo"
    [self.sampleDict setObject:@"FooOne" forKey:@"Key_2"]; // adds @"Foo"
    [self.sampleDict setObject:@"FooTwo" forKey:@"Key_3"]; // adds @"Foo"
    [self.sampleDict setObject:@"FoFour" forKey:@"Key_4"]; // adds @"Foo"
    [self.sampleDict setObject:@"FooFivve" forKey:@"Key_5"]; // adds @"Foo"

    self.arrayTesting = [NSMutableArray new];

    [self.arrayTesting addObject:self.sampleDict];
    NSLog(@"Sample arraytesting>>>>> %@",self.arrayTesting);
}

- (IBAction)buttonAction:(id)sender {



    NSMutableDictionary * toModify = [self.sampleDict mutableCopy];

    [toModify setObject:@"One" forKey:@"Key_1"]; // adds @"Foo"
    [toModify setObject:@"Two" forKey:@"Key_2"]; // adds @"Foo"
    [toModify setObject:@"Three" forKey:@"Key_3"]; // adds @"Foo"
    [toModify setObject:@"FoFour" forKey:@"Key_4"]; // adds @"Foo"
    [toModify setObject:@"FooFivve" forKey:@"Key_5"]; // adds @"Foo"

    [self.arrayTesting addObject:toModify];
    NSLog(@"Sample arraytesting>>>>> %@",self.arrayTesting);
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end

I hope this helps you