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;
}
)
Here you are using same reference of same sampleDict. Instead of that create a new Object again and add.
smapleDict = [NSMutableArray new]