两个日期之间实体属性的核心数据总和



我有一个实体具有amount属性[NSNumber double]。。。一个NSDate属性和两个布尔属性(存储为NSNumber)。。。有没有一种方法可以获得"amount"属性的总和,使日期属性在2个给定日期之间,并且其中一个布尔属性设置为YES。。。我猜这可以在不获取所有实体并将它们添加到for each中的情况下完成。。。

 // 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

您想要查看NSExpressionNSPredicate。这是一个过时但有用的链接,其中有一些信息可以让你开始。

相关内容

最新更新