I would create a new dict containing two elements: a dict inventory and a quantity. I would add an itemArray element like this (untested, so beware of typos):
BOOL found = NO; for (NSDictionary *dict in itemsArray) { if ([[dict objectForKey: @"inventorydict"] isEqual: inventory.itemDict]) { [dict setObject: [NSNumber numberWithInt: [[dict objectForKey: @"quantity"] intValue] + 1] forKey: @"quantity"]; found = YES; break; } } if (!found) { [itemsArray addObject: [NSMutableDictionary dictionaryWithObjectsAndKeys: inventory.itemDict, @"inventorydict", [NSNumber numberWithInt: 1], @"quantity", nil]]; }
Thus, Array elements contain NSDictionaries with two keys: "inventorydict" and "quantity". "inventorydict" is the dict you submitted containing the item that the user bought, and "quantity" is NSNumber. When you receive a new product in the basket, first check if the item is in the array. If so, you add it to the number "quantity", otherwise you will create a new dictionary with an inventory element and quantity 1.
source share