Increasing the number of duplicate NSDictionary objects in an NSArray?

I am writing a shopping cart application where I have product information sent from the server as an NSDictionary object with a position number, description, price as values ​​for their keys. I add all these Dictionary objects to a mutable array and display them in a table. To manually increase the number of each element, I added the Key element to the dictionary object.

NSMutableDictionary* itemDictionary = [[NSMutableDictionary alloc] initWithDictionary:inventory.itemDict]; [itemDictionary setObject:[NSString stringWithFormat:@"1"] forKey:@"Quantity"]; [itemsArray addObject:itemDictionary]; [itemDictionary release]; [self.tableView reloadData]; 

How to increase the value for the Quantity key if there is a duplicate record of the same element? If I add the same element to the array, I get a duplicate. How to find a repeating element, i.e. An element with the same price, description and position number when ignoring the value of the dictionary quantity key when searching for duplicates.

+4
source share
4 answers

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.

+3
source

Store dictionaries in an NSCountedSet . Then you can get the quantity through -countForObject: Use the array for presentation purposes only so you can sort the values ​​in a reasonable way. Rebuild this array whenever the set changes, something like this:

 - (void)itemsDidChange { NSCountedSet *itemSet = [self itemSet]; NSMutableArray *sortedItems = [[NSMutableArray alloc] init]; for (NSDictionary *item in itemSet) { NSUInteger count = [itemSet countForObject:item]; NSNumber *countNum = [[NSNumber alloc] initWithUnsignedInteger:count]; NSMutableDictionary *arrayItem = [item mutableCopy]; [arrayItem setObject:countNum forKey:KEY_QUANTITY]; [countNum release]; [sortedItems addObject:arrayItem]; [arrayItem release]; } [sortedItems sortUsingComparator:/* your comparator here */]; [self setRowItems:sortedItems]; [[self tableView] reloadData]; } 

Even simpler, use the object directly in your array without changing it at all. When you present a quantity to a user in the user interface, just request an itemSet for counting and use that. Then, the array is used solely to put order on the given elements.

+2
source

FYI for setObject: you do not need to use stringWithFormat: if you do not have an object to add to it, you can simply use setObject:@"1" .

If you want to increase the Number , you must use setObject:[NSNumber numberWithInt:1] .

+1
source

based on Rudi's mail, did it like this:

 -(void)addToCart:(id)sender { appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate]; BOOL found = NO; NSString *itemName = // get your item name here [theDict setObject:itemName forKey:@"name"]; if ([appDelegate.theCartArray isEqual:nil]) { [appDelegate.theCartArray addObject:theDict]; } else //implementing Rudy idea { for (theDict in appDelegate.theCartArray) { if ([[theDict objectForKey:@"name"] isEqual:itemName]) { [theDict setObject: [NSNumber numberWithInt: [[theDict objectForKey: @"quantity"] intValue] + 1] forKey: @"quantity"]; found = YES; break; } } if (!found) { [appDelegate.theCartArray addObject: [NSMutableDictionary dictionaryWithObjectsAndKeys: itemName, @"name", [NSNumber numberWithInt: 1], @"quantity", nil]]; } } } 
+1
source

All Articles