Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8760595
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T15:05:12+00:00 2026-06-13T15:05:12+00:00

I have a NSMutableArray with objects of type NSMutableDictionary , the NSMutableDictionary contains 2

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T15:05:16+00:00Added an answer on June 13, 2026 at 3:05 pm

    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a NSMutableArray called putNumberUsed. It contains the following objects @blah1,@blah2,@blah3,@blah4. I want
I have a NSMutableArray containing custom objects of type Episode. Each of these objects
I have a NSMutableArray of objects, below is the struct: //object at index i
I have an NSMutableArray which has five objects in it. I want to insert
I have a NSMutableArray pages with several objects (instances of Page ). Each Page
I have an NSMutableArray that is populated with objects of strings. For simplicity sake
I have been working with NSArray s and NSMutableArray s that store NSDate objects
I have a NSMutableArray of QuoteMap objects. When I add one using the below
I have NSMutableArray with different objects in it of different classes. Now I want
i have 2 NSMutableArray with same type. i want to add second nsmutablearray to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.