1

金額と注文を含む Orders 配列を持つ Dictionary があります 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];
    }
}

これにより、次のようなdictが得られます。

{
    "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

文字列を解析して、数値である注文数量と、文字列のままである可​​能性のあるアイテムIDの2つの情報を抽出する必要があります。

NSMutableDictionaryを使用して、アイテムIDを現在の注文数量を表す数値にマッピングします。それ以外の場合は、古い合計を取得して現在の注文に追加してから、ディクショナリを更新します。

最後に、辞書を繰り返し、各キーと値のペアを文字列に変換し直します。

于 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 に答える