2012-08-10 211 views
1

添加副本有字典與訂單陣列數量和訂單它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"

我想某種子的檢查,如果產品是相同的,但不知道如何獲取正確的數量從重複的順序添加

非常感謝

回答

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,不要忘記發佈!

+0

哇,我深深地謙虛.....這工作很好,我學到了很多......我正準備着手自己編寫一些非常難看的代碼....不知道沒有SO社區我會做什麼....我想寫難看的代碼。非常感謝。 – AhabLives 2012-08-10 21:55:39

0

您需要解析字符串以提取兩條信息:訂單數量(數字值)和商品標識(可能保留一個字符串)。

使用NSMutableDictionary將物品標識映射到表示當前訂單數量的數字值,否則檢索舊的總計並將當前訂單添加到其中,然後更新字典。

最後迭代字典並將每個鍵值對轉換回字符串。

+0

不知道我聽說過使用過的Dictionay映射嗎有一個簡單的鏈接例子,我可以閱讀嗎?謝謝 – AhabLives 2012-08-10 19:46:25

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; 
} 
+0

使用'allKeys:'然後掃描那個數組是非常低效的。只要調用'objectForKey:',如果你得到'nil'就意味着密鑰不在那裏。 – 2012-08-10 20:28:36