0

I'm just getting through tons of research and tutorials on ios app development. Making my first app now and using array with tableviews. My question is now that I have populated arrays with custom objects, I want to query it. Group By with Sum... things of that nature.

In my research I've found predicates for some types of filtering. So far I have successfully returned a new array where on property in my class is equal to "x". Predicate worked for that part.

But I'm not sure how to expand to groupby/sum. I've been reading core data might accomplish this but is probably overkill.

Can someone please help me out with some options to research?

Thanks!!!

example Person Class name age salary

how can I group by name, and at the same time sum up the salaries?

I found this also ... is this an efficient way to tackle problem

// 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);

}

9
  • 1
    You can use @sum: stackoverflow.com/questions/11927151/… Commented Feb 13, 2015 at 17:52
  • thanks, I'll take a look at it. seems simple enough.. predicate, then operator. is this the way you do this on ios for all queries? in .net I use linq a lot, is this the same type of thing? also, that example doesn't work for group by? Commented Feb 13, 2015 at 17:55
  • I didn't do a lot a .Net, so I can't say. For the Group By, it's been a while since I did SQL and stuff like that, so I can't say since I don't remember exactly what it does. Commented Feb 13, 2015 at 17:57
  • 1
    NSPredicate doesn't support grouping. That said you can use NSFetchedResultsController in conjunction with CoreData and an NSPredicate to provide grouping as well as dynamic table management. Look around for some tutorials, Ray Wanderlich's site is a reliable source. Commented Feb 13, 2015 at 18:06
  • 1
    KVC Collection Operators can create collections from your collections depending on common keypaths. Commented Feb 14, 2015 at 1:47

1 Answer 1

1

You probably want to use a dictionary so you can efficiently look up the running total for each person by name. Since you're new at Objective-C, I'll spell everything out:

NSMutableDictionary *totalSalaryForName = [NSMutableDictionary dictionary];
for (Person *person in people) {
    NSString *name = [person name];
    int total = [person salary];
    NSNumber *priorTotal = [totalSalaryForName objectAtIndex:name];
    if (priorTotal != nil) {
        total += [priorTotal intValue];
    }
    NSNumber *newTotal = [NSNumber numberWithInt:total];
    [totalSalaryForName setObject:newTotal forKey:name];
}

However, modern Objective-C provides some “syntactic sugar” you can use to shorten the code:

NSMutableDictionary *totalSalaryForName = [NSMutableDictionary dictionary];
for (Person *person in people) {
    NSString *name = person.name;
    int total = person.salary;
    NSNumber *priorTotal = totalSalaryForName[name];
    if (priorTotal != nil) {
        total += priorTotal.intValue;
    }
    totalSalaryForName[name] = @(total);
}

Now you have a dictionary that maps each person's name to total salary.

Sign up to request clarification or add additional context in comments.

2 Comments

thanks Rob. to take it a step further would a dictionary be able to hold a second property to sum up? say for example... adding up the salary, but also adding another property such as the age. (i know doesnt make sense for age but you get my drift)
You'll need to create a summary class or struct to hold the multiple values, and use instances of that class/struct as the value in the dictionary. If you use a struct, you'll have to wrap it in NSValue to store it in the dictionary.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.