1

我有一个NSMutableArraywith 类型的对象NSMutableDictionary

包含NSMutableDictionary2 个键

-航空公司(字符串)

-评分(整数)

我有NSMutableArray所有对象,我需要对所有航空公司重复对象的评级求和,例如:

Airline  Rating

  A         2

  B         3

  B         4

  C         5

最终结果数组将是 A = 2、C = 5 和等于 7 的 B 之和。

到目前为止我的代码:

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

4

3 回答 3

5

如果您不想更改存储数据的方式,以下是使用键值编码的方式这是@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
于 2012-10-29T16:32:56.340 回答
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

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

于 2012-10-29T16:12:36.127 回答
1

@试试这个

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]);
}
于 2012-10-29T16:16:47.190 回答