0

我有一个实体,它有一个金额属性 [NSNumber double] ...一个 NSDate 属性和 2 个布尔属性(存储为 NSNumber)...有没有办法获得“金额”属性的总和,使得日期属性在 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
4

1 回答 1

0

您想查看NSExpressionNSPredicate是一个过时但有用的链接,其中包含一些帮助您入门的信息。

于 2012-09-10T15:02:32.053 回答