两个日期之间实体属性的核心数据总和
Posted
技术标签:
【中文标题】两个日期之间实体属性的核心数据总和【英文标题】:Core Data sum of entity attributes between two date 【发布时间】:2012-09-10 14:21:12 【问题描述】:我有一个实体,它有一个金额属性 [NSNumber double] ...一个 NSDate 属性和 2 个布尔属性(存储为 NSNumber)...有没有办法获得“金额”属性的总和,这样date 属性在 2 个给定日期之间,并且其中一个布尔属性设置为 YES... 我猜它可以在不获取所有实体并将它们添加到每个实体的情况下完成...
// sum of expenses this month
NSFetchRequest *fetchMonthExp = [[NSFetchRequest alloc] init];
NSEntityDescription *entityExp = [NSEntityDescription entityForName:kTransaction inManagedObjectContext:self.context];
[fetchMonthExp setEntity:entityExp];
NSSortDescriptor *sort = [[NSSortDescriptor alloc]
initWithKey:kDate ascending:NO];
[fetchMonthExp setSortDescriptors:[NSArray arrayWithObject:sort]];
NSPredicate *predFetchEx = [NSPredicate predicateWithFormat:@"(isExpense = YES) AND (date > %@) AND (date < %@) AND (isPendingScheduledTransaction = NO) AND (isDebtRepaiment = NO) AND (isLoanRepaiment = NO) AND (isDebt = NO) AND (isLoan = NO)",[self beginingOfMonthForDate:[NSDate date]],[NSDate date]];
[fetchMonthExp setPredicate:predFetchEx];
NSExpression *ex = [NSExpression expressionForFunction:@"sum:"
arguments:[NSArray arrayWithObject:[NSExpression expressionForKeyPath:@"totalAmountInDefaultCurrency"]]];
NSExpressionDescription *ed = [[NSExpressionDescription alloc] init];
[ed setName:@"resultExpSum"];
[ed setExpression:ex];
[ed setExpressionResultType:NSDoubleAttributeType];
NSArray *properties = [NSArray arrayWithObject:ed];
[fetchMonthExp setPropertiesToFetch:properties];
NSError *error1 = nil;
NSArray *resultsEx = [self.context executeFetchRequest:fetchMonthExp error:&error1]; <- EXC_BAD_ACCESS (code = 2, address = 0x0)
if(resultsEx == nil)
NSLog(@"Failed to fetch exp sum: %@", [error1 localizedDescription]);
NSArray* detailedErrors = [[error1 userInfo] objectForKey:NSDetailedErrorsKey];
if(detailedErrors != nil && [detailedErrors count] > 0)
for(NSError* detailedError in detailedErrors)
NSLog(@" DetailedError: %@", [detailedError userInfo]);
那个错误是怎么回事....
-[UIView count]: unrecognized selector sent to instance 0x7856150
【问题讨论】:
【参考方案1】:您想查看NSExpression
和NSPredicate
。 Here 是一个过时但有用的链接,其中包含一些帮助您入门的信息。
【讨论】:
以上是关于两个日期之间实体属性的核心数据总和的主要内容,如果未能解决你的问题,请参考以下文章