First question: Basically I'm attempting to add NSMutableDictionary objects to an NSMutableArray by using a method addItem.
-(void)addItem:(id)ObID name:(id)ObName description:(id)ObDesc menuName:(id)ObmName menuID:(id)ObmID mid:(id)Obmid pod:(id)pod type:(id)obType price:(id)price
{
NSMutableDictionary *itemToAdd = [[NSMutableDictionary alloc]init];
[itemToAdd setValue:ObID forKey:@"id"];
[itemToAdd setValue:ObName forKey:@"name"];
[itemToAdd setValue:ObDesc forKey:@"description"];
[itemToAdd setValue:ObmName forKey:@"mname"];
[itemToAdd setValue:ObmID forKey:@"menu_id"];
[itemToAdd setValue:Obmid forKey:@"mid"];
[itemToAdd setValue:pod forKey:@"POD"];
[itemToAdd setValue:obType forKey:@"Type"];
[itemToAdd setValue:price forKey:@"Price"];
[itemToAdd setValue:@"1" forKey:@"Amount"];
[items addObject:itemToAdd];
}
However when this method is called again later the next object that is added to the overwrites the value of an object with the same keys. Im aware this is because an array simply retains a memory address, hence when the same object simply with a different type "obType" is added all of those values change.
How am I able to add objects to an NSMutableArray without them referencing each other?
Example:
Output after adding one object:
{
Amount = 1;
POD = BOTH;
Type = {
0 = Vegetarian;
1 = Aussie;
};
description = "Online Special Only - Two Large Pizza's $25 (No half halves or extra toppings!) Enjoy!";
id = 7596;
"menu_id" = 112;
mid = 112;
mname = "Deal";
name = "Hungry? Two Large Pizzas!";
}
)
Output after adding another object of same ID however of a different type:
{
Amount = 1;
POD = BOTH;
Type = {
0 = "Plain";
1 = Aussie;
};
description = "Online Special Only - Two Large Pizza's $25 (No half halves or extra toppings!) Enjoy!";
id = 7596;
"menu_id" = 112;
mid = 112;
mname = "Deal";
name = "Two Large Pizzas!";
},
{
Amount = 1;
POD = BOTH;
Type = {
0 = "Plain";
1 = Aussie;
};
description = "Online Special Only - Two Large Pizza's $25 (No half halves or extra toppings!) Enjoy!";
id = 7555;
"menu_id" = 112;
mid = 112;
mname = "Deal";
name = Two Large Pizzas!";
}
)
As can be seen both object types have changed.
Ok so I solved the problem, not 100% sure why. I was passing in the type object as an NSMutableDictionary which if an item had 2 variations, one object in the dictionary would have a key of 0 the other 1 when passed in like this they overwrite each other. Passing just the values in as an NSArray fixes this.
Done by passing in:
NSArray * values = [selectedItemOptions allValues];
Thanks all who helped.