0
votes

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

}

1
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? - solarissf
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. - Larme
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. - David Berry
KVC Collection Operators can create collections from your collections depending on common keypaths. - stevesliva

1 Answers

1
votes

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.