NSMutableArray上的Sum复制

我有一个NSMutableArrayNSMutableDictionary类型的对象,

NSMutableDictionary包含2个键

– 航空公司(String)

-Rating(整数)

我有一个带有所有对象的NSMutableArray ,我需要的是总结所有航空公司重复对象的评级,例如:

 Airline Rating A 2 B 3 B 4 C 5 

最终结果数组将是A = 2,C = 5和B的总和等于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 ,任何帮助将不胜感激,在此先感谢!

如果您不想改变存储数据的方式,那么您将如何使用键值编码来实现它。 下面是@distinctUnionOfObjects和@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 

如果可能的话,我建议完全重新设计。

创建一个classAirline

 @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’中你只需持有一个NSArray其中包含你的Airline对象的实例。 它可能需要您更改一些现有代码,但我认为从长远来看它可以节省您的时间和麻烦。 也许您稍后会认识到,您想要为您的航空公司添加其他属性。 字典是一种繁琐的方法。

@尝试这个

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