从核心数据中检索已过滤记录的总和,并将结果显示到标签

时间:2013-10-16 23:10:28

标签: ios core-data

我想查询我的实体“DIncome”来对“信用”属性求和但是我只想要将其“daystamp”属性等于今天的entires。下面的代码让我接近,但我不明白如何完成此代码,以便将信用DIncome属性的总和输出到我的视图控制器上的标签。

继承人我得到了什么;

//Get today date
NSDate *dateSelect = [NSDate date];

//formate today date as day only "dd"
NSDateFormatter *dfdd = [[NSDateFormatter alloc]init];
[dfdd setDateFormat:@"dd"];
NSString *formattedDatedd = [dfdd stringFromDate:dateSelect];

//Fetchrequest
NSFetchRequest *request = [[NSFetchRequest alloc] init];

NSEntityDescription *entity = [NSEntityDescription entityForName:@"DIncome" inManagedObjectContext:context];
[request setEntity:entity];

// Specify that the request should return dictionaries.
[request setResultType:NSDictionaryResultType];

// Create an expression for the key path.
NSExpression *keyPathExpression = [NSExpression expressionForKeyPath:@"credit"];

NSExpression *sumExpression = [NSExpression expressionForFunction:@"sum:" arguments:[NSArray arrayWithObject:keyPathExpression]];

// Create an expression description using the minExpression and returning a date.
NSExpressionDescription *expressionDescription = [[NSExpressionDescription alloc] init];

// The name is the key that will be used in the dictionary for the return value.
[expressionDescription setName:@"sumCredit"];
[expressionDescription setExpression:sumExpression];
[expressionDescription setExpressionResultType:NSDateAttributeType];

// Set the request's properties to fetch just the property represented by the expressions.
[request setPropertiesToFetch:[NSArray arrayWithObject:expressionDescription]];

// Execute the fetch.
NSError *error = nil;
NSArray *objects = [context executeFetchRequest:request error:&error];
if (objects == nil) {
    // Handle the error.
}
else {
    if ([objects count] > 0) {
        NSLog(@"Credit Sum: %@", [[objects objectAtIndex:0] valueForKey:@"sumCredit"]);
        self.dayDisplayLabel.text = sumExpression;
    }
}

对于问题的第二部分,我需要将信用额的总和设置为我的dayDisplayLabel 这不起作用,因为它是一个不兼容的指针,将NSString分配给NSExpression

self.dayDisplayLabel.text = sumExpression;

我该如何做到这一点?

3 个答案:

答案 0 :(得分:0)

您可以将[request setResultType:NSDictionaryResultType]propertiesToFetch一起使用,以仅检索托管对象的某些属性。如果要限制记录的记录,可以使用NSPredicate(类似于SQL语句中的WHERE子句)。

答案 1 :(得分:0)

获取请求将其结果作为托管对象数组返回。您可以从这些对象中获取属性值。在您的情况下,您将获得表示DIncome实体类型的托管对象数组。如果您将NSManagedObject子类化,则这些是您的子类的实例;如果你没有子类,那么它们就是NSManagedObject的实例。

要从这些实例中获取字符串属性的值,请使用键值编码。如果您的属性被命名为“title”,则您将获得第一个对象的字符串值:

NSString *firstTitle = [mutableFetchResults[0] valueForKey:@"title"];

使用类似的代码获取任何其他属性的值。

如果您希望获取请求仅包含DIncome实例的子集,请查看NSPredicate。您可以像SQL“WHERE”子句一样使用它来仅请求符合特定条件的实例,例如:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name = %@", someNameString];
[request setPredicate:predicate];

答案 2 :(得分:0)

为了过滤您的查询结果,您需要为您的请求设置setPredicate

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"daystamp = %@", formattedDatedd];
[request setPredicate:predicate];

要将此显示在您的标签上,请使用您在NSLog行中使用的相同行。

[[objects objectAtIndex:0] valueForKey:@"sumCredit"]

如果看起来像这样,

self.dayDisplayLabel.text = [[objects objectAtIndex:0] valueForKey:@"sumCredit"];

希望这有帮助!

相关问题