NSMutableArray上的Sum复制

时间:2012-10-29 15:40:18

标签: iphone ios nsmutablearray nsmutabledictionary

我有一个NSMutableArray,其对象类型为NSMutableDictionary

NSMutableDictionary包含2个键

- 航空公司(字符串)

-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,任何帮助将不胜感激,在此先感谢!

3 个答案:

答案 0 :(得分: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

答案 1 :(得分: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

然后在“主要类”中,您只需拥有NSArrayAirline个实例的实例。它可能需要您更改一些现有代码,但我认为从长远来看它可以节省您的时间和麻烦。也许您稍后会认识到,您想要为您的航空公司添加其他属性。字典是一种繁琐的方法。

答案 2 :(得分:1)

@try this

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