Skip to content

Instantly share code, notes, and snippets.

@hachinobu
Created May 9, 2014 10:30
Show Gist options
  • Save hachinobu/998b8ae26065b8e069c2 to your computer and use it in GitHub Desktop.
Save hachinobu/998b8ae26065b8e069c2 to your computer and use it in GitHub Desktop.
CoreDataでcount関数を使う
//集計対象
NSExpression *keyPathExpression = [NSExpression expressionForKeyPath:@"column1"];
//集計関数countを指定
NSExpression *countExpression = [NSExpression expressionForFunction:@"count:" arguments:@[keyPathExpression]];
//集計式の対象(NSExpressionDescription )
NSExpressionDescription *expressionDescription = [[[NSExpressionDescription alloc] init] autorelease];
[expressionDescription setName:@"count"];
[expressionDescription setExpression:countExpression];
[expressionDescription setExpressionResultType:NSInteger32AttributeType];
[fetchRequest setPropertiesToGroupBy:@[column2]];
[fetchRequest setPropertiesToFetch:@[column2, expressionDescription]];
[fetchRequest setResultType:NSDictionaryResultType];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment