1

我有一个带有 Orders 数组的字典,其中包含金额和订单 Orders = ("3 White Shirts", "8 White Shirts", "4 blue shorts")

我将如何遍历它并添加重复订单的数量,以便结果字符串或数组是 Orders = ("11 White Shirts", "4 blue shorts") or myString ="11 White Shirts, 4 blue shorts"

我正在考虑某种子字符串来检查产品是否相同,但不确定如何从重复订单中获取要添加的正确数量

非常感谢

4

3 回答 3

2

好的,这是一种方法(我能想到的最短的方法):

// Assuming that 'orders' is the array in your example
NSMutableDictionary *orderDict = [[NSMutableDictionary alloc] init]; 

for (NSString *order in orders)
{
    // Separate the string into components
    NSMutableArray *components = [[order componentsSeparatedByString:@" "] mutableCopy];

    // Quantity is always the first component
    uint quantity = [[components objectAtIndex:0] intValue];
    [components removeObjectAtIndex:0];

    // The rest of them (rejoined by a space is the actual product)
    NSString *item = [components componentsJoinedByString:@" "];

    // If I haven't got the order then add it to the dict
    // else get the old value, add the new one and put it back to dict
    if (![orderDict valueForKey:item])
        [orderDict setValue:[NSNumber numberWithInt:quantity] forKey:item];
    else{
        uint oldQuantity = [[orderDict valueForKey:item] intValue];
        [orderDict setValue:[NSNumber numberWithInt:(oldQuantity+quantity)] forKey:item];
    }
}

这会给你一个这样的字典:

{
    "White Shirts" = 11;
    "blue shorts" = 4;
}

因此,您可以遍历键并生成一个字符串数组,如下所示:

NSMutableArray *results = [[NSMutableArray alloc] initWithCapacity:0];
for (NSString *key in [orderDict allKeys])
{
    [results addObject:[NSString stringWithFormat:@"%@ %@", [orderDict valueForKey:key], key]];
}

最后会给你:

(
    "11 White Shirts",
    "4 blue shorts"
) 

PS。如果您不使用 ARC,请不要忘记释放!

于 2012-08-10T20:31:49.550 回答
0

您需要解析字符串以提取两条信息:订单数量,它是一个数值,以及项目标识,它可能仍然是一个字符串。

使用 NSMutableDictionary 将项目标识映射到表示当前订单数量的数值,否则检索旧总数并将当前订单添加到其中,然后更新字典。

最后迭代字典并将每个键值对转换回字符串。

于 2012-08-10T19:42:33.240 回答
0

因为它看起来像你的数组包含字符串对象,我会做这样的事情:

#import <Foundation/Foundation.h>

int main(int argc, const char * argv[])
{

    @autoreleasepool {

        NSArray *ordersAsStrings = [NSArray arrayWithObjects:@"7 white shirts", @"4 blue jeans", @"3 white shirts", @"4 blue jeans", nil];
        NSMutableDictionary *combinedQuantities = [NSMutableDictionary new];
        NSMutableArray *combinedOrdersAsStrings = [NSMutableArray new];

        // take each string and break it up into the quantity and the item
        for (NSString *orderAsString in ordersAsStrings) {
            NSInteger scannedQuantity = 0;
            NSString *scannedItem = nil;
            NSScanner *scanner = [NSScanner scannerWithString:orderAsString];
            [scanner scanInteger:&scannedQuantity];
            [scanner scanCharactersFromSet:[[NSCharacterSet illegalCharacterSet] invertedSet] intoString:&scannedItem];

            // if the item is already in combinedOrders
            if ([combinedQuantities.allKeys containsObject:scannedItem] == YES) {
                // update quantity
                NSNumber *existingQuantity = [combinedQuantities objectForKey:scannedItem];
                NSInteger combinedQuantity = existingQuantity.integerValue + existingQuantity.integerValue;
                [combinedQuantities setObject:[NSNumber numberWithInteger:combinedQuantity] forKey:scannedItem];
            } else {
                // otherwise add item
                NSNumber *quantity = [NSNumber numberWithInteger:scannedQuantity];
                [combinedQuantities setObject:quantity forKey:scannedItem];
            }
        }

        // change combined quantities back into strings
        for (NSString *key in combinedQuantities.allKeys) {
            NSNumber *quantity = [combinedQuantities objectForKey:key];
            NSString *orderAsString = [NSString stringWithFormat:@"%ld %@", quantity.integerValue, key];
            [combinedOrdersAsStrings addObject:orderAsString];
        }

        NSLog(@"Combined Orders: %@", combinedOrdersAsStrings);
    }
    return 0;
}
于 2012-08-10T20:09:12.627 回答