2012-09-28 17 views
12

私は同様の質問に出くわしましたが、どのようにそれを達成するかの完全な例はありませんでした。GROUP BY SUM()with Core Data

私が翻訳しようとしているSQLクエリはこれです:

SELECT date, SUM(amount) FROM Table GROUP BY date; 

私は、次の曲のコードをデバッグする助けが必要(現在fetchRequestがnilを返す):

entity = [NSEntityDescription entityForName:@"Table" inManagedObjectContext:self.managedObjectContext]; 
[fetchRequest setEntity:entity]; 

NSExpressionDescription* ex = [[NSExpressionDescription alloc] init]; 
[ex setExpression:[NSExpression expressionWithFormat:@"@sum.amount"]]; 
[ex setExpressionResultType:NSDecimalAttributeType]; 

[fetchRequest setPropertiesToFetch:[NSArray arrayWithObjects:@"date", ex, nil]]; 
[fetchRequest setPropertiesToGroupBy:[NSArray arrayWithObject:@"date"]]; 
[fetchRequest setResultType:NSDictionaryResultType ]; 

[self.managedObjectContext executeFetchRequest:fetchRequest error:&error]; 

これは誤りです。

2012-09-28 13:58:46.319 App[12205:c07] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSArrayM insertObject:atIndex:]: object cannot be nil' 
+0

NSLog(@ "error:%@"、error)を印刷してください。出力 – CarlJ

+0

NSLogに到達しない: 2012-09-28 13:58:46.319 App [12205:c07] ***キャッチされていない例外 'NSInvalidArgumentException'のためアプリを終了しています、理由: '*** - [__NSArrayM insertObject:atIndex:]:オブジェクトはゼロにはできません ' –

+0

例外をキャッチした場合、エラーの内容はnull –

答えて

7

上記のコードは

を必要
[ex setName:@"somename"]; 

fetchRequestを実行する前。