Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/103.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 使用另一个表达式作为sum:expression的参数从NSManagedObject获取聚合数据_Objective C_Ios_Core Data_Nsexpression - Fatal编程技术网

Objective c 使用另一个表达式作为sum:expression的参数从NSManagedObject获取聚合数据

Objective c 使用另一个表达式作为sum:expression的参数从NSManagedObject获取聚合数据,objective-c,ios,core-data,nsexpression,Objective C,Ios,Core Data,Nsexpression,是否可以使用表达式作为sum:expression的参数 我有实体(NSManagedObjectclass)Drink,属性为costPerDrink,numberOfDrinks和drinkingDate 我想得到一段时间内总成本的总和(numberOfDrinks乘以costPerDrink)。虽然我在获取单个属性的和(例如时间段内numberOfDiverses的和)时没有问题,但当我尝试在另一个(乘法)表达式上使用和表达式时,我得到了错误: NSInvalidArgumentExcep

是否可以使用表达式作为
sum:
expression的参数

我有实体(
NSManagedObject
class)
Drink
,属性为
costPerDrink
numberOfDrinks
drinkingDate

我想得到一段时间内总成本的总和(
numberOfDrinks
乘以
costPerDrink
)。虽然我在获取单个属性的和(例如时间段内
numberOfDiverses
的和)时没有问题,但当我尝试在另一个(乘法)表达式上使用和表达式时,我得到了错误:

NSInvalidArgumentException, reason: Unsupported argument to sum : 
(
    "costPerDrink * numberOfDrinks" 
)
见代码:

// Init fech
NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Drink" inManagedObjectContext:[Service appContext]];
[request setEntity:entity];

// Return dictionary
[request setResultType:NSDictionaryResultType];

// Set conditions
[request setPredicate:[NSPredicate predicateWithFormat:@"(drinkingDate>=%@) AND (drinkingDate<=%@)", self.startDate, self.endDate]];

// Set 1st column to be returned - count of drinks
NSExpression *drinksExpression = [NSExpression expressionForKeyPath:@"numberOfDrinks"];
NSExpression *sumDrinksExpression = [NSExpression expressionForFunction:@"sum:" arguments:[NSArray arrayWithObject:drinksExpression]];
NSExpressionDescription *sumDrinksResultDescription = [[NSExpressionDescription alloc] init];
[sumDrinksResultDescription setName:@"sumDrinks"];
[sumDrinksResultDescription setExpression:sumDrinksExpression];
[sumDrinksResultDescription setExpressionResultType:NSInteger32AttributeType];

// Set 2nd column to be returned - total cost
NSExpression *costExpression = [NSExpression expressionForKeyPath:@"costPerDrink"];
NSExpression *totalExpression = [NSExpression expressionForFunction:@"multiply:by:"
                                                            arguments:[NSArray arrayWithObjects:costExpression,
                                                                                          drinksExpression, nil]];
NSExpression *sumCostExpression = [NSExpression expressionForFunction:@"sum:" arguments:[NSArray arrayWithObject:totalExpression]];
NSExpressionDescription *sumCostResultDescription = [[NSExpressionDescription alloc] init];
[sumCostResultDescription setName:@"sumCost"];
[sumCostResultDescription setExpression:sumCostExpression];
[sumCostResultDescription setExpressionResultType:NSDecimalAttributeType];

// Add expressions to fetch
[request setPropertiesToFetch:[NSArray arrayWithObjects: sumDrinksResultDescription, sumCostResultDescription, nil]];

// Execute fech
NSError *error;
NSArray *result = [[Service appContext] executeFetchRequest:request error:&error];
//初始化fech
NSFetchRequest*request=[[NSFetchRequest alloc]init];
NSEntityDescription*entity=[NSEntityDescription entityForName:@“Drink”inManagedObjectContext:[Service appContext]];
[请求集合实体:实体];
//返回字典
[请求setResultType:NSDictionaryResultType];
//设定条件

[request setPredicate:[NSPredicate Predicate WithFormat:@”(drinkingDate>=%@)和(drinkingDate)我过去也遇到过类似的问题,我按照这里的说明手动计算了它。但是,如果有任何核心数据专家对此作出答复,我将非常高兴。