Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I came across similar questions but none of them had a complete example of how to accomplish it.

The SQL query I am trying to translate is this:

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

I need help debugging the following piece code (currently the fetchRequest returns 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];

This is the 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'
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
413 views
Welcome To Ask or Share your Answers For Others

1 Answer

The code above needs

[ex setName:@"somename"];

before executing the fetchRequest.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...