问题
I have a NSMutableArray
with objects of type NSMutableDictionary
,
the NSMutableDictionary
contains 2 keys
-Airlines (string)
-Rating (integer)
I have an NSMutableArray
with all the objects and what i need is to Sum the rating of all the airline companies repeated objects, an example:
Airline Rating
A 2
B 3
B 4
C 5
The end result array will be the A = 2, C = 5 and the Sum of B´s that is equal to 7.
My code so far:
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];
}
}
}
A bit messy i know but i dont know how to work with NSMutableDictionary
, any help will be much appreciated, Thanks in Advance!
回答1:
Incase you dont want to change how your storing the data, heres how you would do it using key-value coding. Heres the dirrect link to the documentation for @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);
}
This gives the following output
A: 2
B: 7
C: 5
回答2:
I would suggest to redesign that entirely if that's at all possible.
Create a class 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
Then in your 'mainclass' you simply hold an NSArray
with instances of your Airline
objects. It might require you to change some of your existing code, but I think in the long run it saves you time and trouble. Perhaps you recognize later on, that you want to add additional properties to your Airlines. A dictionary is a cumbersome way to do that.
回答3:
@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]);
}
来源:https://stackoverflow.com/questions/13124857/sum-duplicate-on-nsmutablearray