【问题标题】:Sum duplicate on NSMutableArray在 NSMutableArray 上求和重复
【发布时间】:2012-10-29 15:40:18
【问题描述】:

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

NSMutableDictionary 包含 2 个键

-航空公司(字符串)

-评分(整数)

我有一个包含所有对象的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,任何帮助将不胜感激,在此先感谢!

【问题讨论】:

  • 你能把它的日志贴出来吗???你的解释有点令人困惑......所以如果我们能看到你的“arrayMeatRating”数组日志的日志会更好

标签: iphone ios nsmutablearray nsmutabledictionary


【解决方案1】:

如果您不想更改存储数据的方式,以下是使用 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 没有意识到这一点。非常好! (如果它起作用了 ;))我想我还是会重新设计,但这很棒。
【解决方案2】:

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

创建一个类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

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

【讨论】:

    【解决方案3】:

    @试试这个

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-05-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-20
      相关资源
      最近更新 更多