Iphone NSArray中重复NSDictionary对象的增量计数?

Iphone NSArray中重复NSDictionary对象的增量计数?,iphone,objective-c,ios,Iphone,Objective C,Ios,我正在编写一个购物车应用程序,其中我将商品信息作为NSDictionary对象从服务器发回,商品编号、描述、价格作为其密钥的值。我将所有这些字典对象添加到可变数组中,并显示在表视图中。为了手动增加每个项目的数量,我向项目字典对象添加了数量键 NSMutableDictionary* itemDictionary = [[NSMutableDictionary alloc] initWithDictionary:inventory.itemDict]; [itemDictionary setObj

我正在编写一个购物车应用程序,其中我将商品信息作为NSDictionary对象从服务器发回,商品编号、描述、价格作为其密钥的值。我将所有这些字典对象添加到可变数组中,并显示在表视图中。为了手动增加每个项目的数量,我向项目字典对象添加了数量键

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

如果同一项目存在重复条目,如何增加关键数量的值?如果我将相同的项目添加到数组中,我将得到一个重复的项目,如何找到重复的项目,即具有相同价格的项目,在搜索重复项时忽略字典数量键值的说明和项目编号

setObject:
参考您不需要使用
stringWithFormat:
如果您没有要添加的对象,只需使用
setObject:@“1”


如果要增加数量,则应使用
设置对象:[NSNumber numberwhithint:1]

我将创建一个包含两项的新目录:库存目录和数量。我会向itemsArray添加一个项目,如下所示(未经测试,因此请注意拼写错误):


因此itemsArray包含NSDictionary,其中有两个键:“inventorydict”和“quantity”。“inventorydict”是传递给您的包含用户购买的物品的dict,“quantity”是NSNumber。当您收到篮子中的新产品项目时,首先检查该项目是否已在阵列中。如果是,则在“数量”编号中添加一个,否则将创建一个包含库存项和数量1的新词典。

将词典存储在
NSCountedSet
中。然后,您可以通过
-countForObject:
获取数量。仅将数组用于表示目的,因此可以对值进行合理排序。每当集合更改时重新生成此数组,如下所示:

- (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];
}

更简单的是,直接在数组中使用对象,而不进行任何更改。当您在UI中向用户显示数量时,只需查询
itemSet
中的计数,然后使用它。然后,该数组仅用于对集合中的项目施加顺序。

基于Rudy的帖子,它的工作方式如下:

-(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's 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]];
        }
    }
}

setObject:
您不需要使用
stringWithFormat:
如果您没有要添加的对象,只需使用
setObject:@“1”
setObject:[NSNumber numberwhithint:1]
。@WrightCS。如果我将相同的项目添加到数组中,我将得到一个重复的项目,如何查找重复的项目,即具有相同价格、描述和项目编号的项目,同时在搜索重复项时忽略字典的数量键的值?
-(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's 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]];
        }
    }
}