我遇到了类似的问题,但他们都没有一个完整的例子来说明如何完成它。
我要翻译的 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'