2012-09-10 61 views
0

我有一個具有金額屬性的實體[NSNumber double] ...一個NSDate屬性和2個布爾屬性(存儲爲NSNumber)...有沒有辦法獲得「amount」屬性的總和,使得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 

回答

0

你婉t查看NSExpressionNSPredicateHere是一個日期,但有用的鏈接,它有一些信息,讓你開始。

相關問題