2012-10-29 89 views
1

我有NSMutableDictionary類型的對象NSMutableArray薩姆重複上的NSMutableArray

NSMutableDictionary包含2個鍵

-Airlines(字符串)

-Rating(整數)

我有一個NSMutableArray與所有的對象,我需要的是總結所有航空公司重複對象的評級,例如:

Airline Rating 

    A   2 

    B   3 

    B   4 

    C   5 

最終結果陣列將是A = 2,C = 5與B's的薩姆等於7.

到目前爲止我的代碼:

for (int i = 0; i < arrayMealRating.count; ++i) { 
     NSMutableDictionary *item = [arrayMealRating objectAtIndex:i]; 
     NSLog(@"item=%@",item); 
     for (int j = i+1; j < arrayMealRating.count; ++j) 
     { 
      if ([[item valueForKey:@"Airline"] isEqualToString:[arrayMealRating objectAtIndex:j]]){ 
       NSMutableDictionary *item = [arrayMealRating objectAtIndex:j]; 
       NSMutableDictionary *item1 = [arrayMealRating objectAtIndex:i]; 
       NSInteger auxCount = [[item valueForKey:@"setMealRating"] integerValue] + [[item1 valueForKey:@"setMealRating"] integerValue]; 

       NSMutableDictionary *aux = [NSMutableDictionary dictionaryWithObjectsAndKeys:[item valueForKey:@"Airline"], @"Airline" 
                 ,[NSString stringWithFormat:@"%d",auxCount], @"setMealRating" 
                 ,nil]; 
       NSLog(@"aux=%@",aux); 
       [arrayMealRating replaceObjectAtIndex:i withObject:aux]; 

      } 
     } 
    } 

甲有點雜亂我知道,但我不知道如何與NSMutableDictionary,任何幫助將不勝感激,感謝提前!

+0

你可以發佈它的日誌是怎麼???你的解釋不會讓人困惑......因此,如果我們能看到你的「arrayMeatRating」數組日誌 –

回答

5

櫃面你不想改變你如何存儲數據,繼承人你將如何使用key-value coding做到這一點。下文鏈接至@distinctUnionOfObjects and @sum的文檔。

// Get all the airline names with no duplicates using the KVC @distinctUnionOfObjects collection operator 
NSArray *airlineNames = [arrayMealRating valueForKeyPath:@"@distinctUnionOfObjects.Airline"]; 

// Loop through all the airlines 
for (NSString *airline in airlineNames) { 

    // Get an array of all the dictionaries for the current airline 
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(Airline == %@)", airline]; 
    NSArray *airlineMealRating = [arrayMealRating filteredArrayUsingPredicate:predicate]; 

    // Get the sum of all the ratings using KVC @sum collection operator 
    NSNumber *rating = [airlineMealRating valueForKeyPath:@"@sum.Rating"]; 
    NSLog(@"%@: %@", airline, rating); 
} 

這讓下面的輸出

A: 2 
B: 7 
C: 5 
+0

+ 1沒有意識到這一點,那會更好。非常好! (如果它正在工作;))我想我會盡管重新設計,但不過這很棒。 – Tobi

1

我會建議重新設計,如果這是完全可能的。

創建一個類Airline

@interface Airline : NSObject 

@property (strong, nonatomic) NSString *name; 
@property (strong, nonatomic) NSMutableArray *mealRatings; 

- (void)addMealRating:(float)rating; 
- (float)sumOfMealRatings; 

@end 


@implementation 

- (id)initWithName:(NSString *)pName 
{ 
    self = [super init]; 
    if (self) 
    { 
     self.name = pName; 
     self.mealRatings = [NSMutableArray array]; 
    } 
    return self; 
} 

- (void)addMealRating:(float)rating 
{ 
    [self.mealRatings addObject:@(rating)]; 
} 

- (float)sumOfRatings 
{ 
    float sum = 0; 
    for (NSNumber *rating in self.mealRatings) 
    { 
     sum += [rating floatValue]; 
    } 
} 

@end 

然後在你的 'mainclass' 你只需持有一個NSArrayAirline對象的實例。它可能需要您更改一些現有的代碼,但我認爲從長遠來看它可以節省您的時間和麻煩。也許您稍後會認識到,您希望爲您的航空公司添加其他屬性。字典是一個麻煩的方法來做到這一點。

1

@try這

NSMutableArray *myArray = [[NSMutableArray alloc] initWithCapacity:4]; 

NSMutableDictionary *dict = [[NSMutableDictionary alloc] init]; 
[dict setValue:[NSNumber numberWithInteger:2] forKey:@"A"]; 
[myArray addObject:dict]; 

NSMutableDictionary *dict2 = [[NSMutableDictionary alloc] init]; 
[dict2 setValue:[NSNumber numberWithInteger:3] forKey:@"B"]; 
[myArray addObject:dict2]; 

NSMutableDictionary *dict3 = [[NSMutableDictionary alloc] init]; 
[dict3 setValue:[NSNumber numberWithInteger:4] forKey:@"B"]; 
[myArray addObject:dict3]; 

NSMutableDictionary *dict4 = [[NSMutableDictionary alloc] init]; 
[dict4 setValue:[NSNumber numberWithInteger:5] forKey:@"D"]; 
[myArray addObject:dict4]; 


NSMutableDictionary *resultDictionary = [[NSMutableDictionary alloc] init]; 

for(NSMutableDictionary *dictionary in myArray) 
{ 
    NSString *key = [[dictionary allKeys] objectAtIndex:0]; 

    NSInteger previousValue = [[resultDictionary objectForKey:key] integerValue]; 


     NSInteger value = [[dictionary objectForKey:key] integerValue]; 

     previousValue += value; 



    [resultDictionary setObject:[NSNumber numberWithInteger:previousValue] forKey:key]; 

} 

for(NSString *key in resultDictionary) 
{ 
    NSLog(@"value for key = %@ = %d",key, [[resultDictionary valueForKey:key] integerValue]); 
}