在NSArray中如何增加重复的NSDictionary对象计数?

4

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

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

如何在键值为 Quantity 的条目有重复输入的情况下递增其值?如果我将相同的项目添加到数组中,就会得到重复项,如何查找重复项,即具有相同价格、描述和项目编号的项,在搜索重复项时忽略字典中的 Quantity 键的值。

关于 setObject: 方法,如果你没有要添加的对象,就不需要使用 stringWithFormat:。你可以直接使用 setObject:@"1" 或者 setObject:[NSNumber numberWithInt:1] - WrightsCS
如果我向数组中添加相同的项目,我将得到一个重复项,如何找到重复的项目,即在搜索重复项时忽略字典的“Quantity”键的值,同时拥有相同价格、描述和项目编号的项目? - 0x8badf00d
4个回答

3
我会创建一个新的字典,其中包含两个项目:库存字典和数量。我会像这样向itemsArray添加一个项目(未经测试,请注意拼写错误):

我会创建一个新的字典,其中包含两个项目:库存字典和数量。我会像这样向itemsArray添加一个项目(未经测试,请注意拼写错误):

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

所以itemsArray包含两个键“inventorydict”和“quantity”的NSDictionaries。 “inventorydict”是传递给您的包含用户购买的物品的字典,“quantity”是一个NSNumber。当您在篮子中收到新的产品项时,首先检查该项是否已在数组中。如果是,则将“quantity”数字加1,否则创建一个新字典,其中包含库存项和数量为1。

2

将你的字典存储在一个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的计数并使用它即可。数组仅用于对集合项强制执行顺序。


1

根据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]];
        }
    }
}

1

关于 setObject:,如果您没有要添加的对象,那么您不需要使用 stringWithFormat:,您可以直接使用 setObject:@"1"

如果您想增加 数量,则应该使用 setObject:[NSNumber numberWithInt:1]


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接