Advertise here




Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Get value from core data property and sum it

jsantojsanto Posts: 30New Users @
Hi

i´m using the code below to get the values in one of my core data property´s, but i don´t know if it is possible to extract and them sum them, and i was wondering if someone could help me with this:

AppDelegate *appDelegate =[[UIApplication sharedApplication] delegate];
self.contextTotal=[appDelegate managedObjectContext];

NSEntityDescription *entity= [NSEntityDescription entityForName:@CellSave inManagedObjectContext:contextTotal];
NSFetchRequest *request= [[NSFetchRequest alloc] init];
[request setEntity:entity];
[request setResultType:NSDictionaryResultType];
[request setReturnsDistinctResults:YES];
[request setPropertiesToFetch:[NSArray arrayWithObject:@smtrCbrData]];
NSArray *objects= [contextTotal executeFetchRequest:request error:nil];
NSLog (@results: %@",objects);

The results are:


Structure[1595:11603] results: (
{
smtrCbrData = "\U20ac 770.00";
},
{
smtrCbrData = "\U20ac 3,080.00";
}

Is it possible to get the numbers and them sum them? how can i perform this?

Thanks

Replies

  • RickSDKRickSDK Posts: 1,237Registered Users @ @ @ @
    its unclear what you are asking. "objects" is an array of NSManagedObjects. as in many objects. You can then step through them one at a time and extract values based on your schema. Your schema presumably has fields. What field are you referring to?
  • jsantojsanto Posts: 30New Users @
    Hello Rick

    Sorry for taking so long to answer and thanks for commenting. I already discovered a way to get the values from my core data properties, and i had to make some changes(change my properties to double instead of strings)...Meanwhile another problem appears but first let me show you my code:

    NSFetchRequest* request = [NSFetchRequest fetchRequestWithEntityName:@Ent];
    [request setResultType:NSDictionaryResultType];
    [request setReturnsDistinctResults:YES];
    NSExpressionDescription *expressionDescription = [[NSExpressionDescription alloc] init];
    [expressionDescription setName:@count];
    [expressionDescription setExpressionResultType:NSDoubleAttributeType];

    NSExpression *keyPathExpression = [NSExpression expressionForKeyPath:@smtrCbrData];
    NSExpression* expr = [NSExpression expressionForFunction:@sum: arguments:@[keyPathExpression]];
    [expressionDescription setExpression:expr];
    [request setPropertiesToFetch:@[@smtrCbrData,expressionDescription]];

    NSError* error = nil;
    NSArray* objects = [contextApuramento executeFetchRequest:request error:&error];

    if (objects.count >0)
    {
    id value = [[objects valueForKey:@count]objectAtIndex:0];
    self.cobCstTl.text = [formatter stringFromNumber:[NSNumber numberWithDouble:[value doubleValue]]];
    NSLog (@results: %@",objects);
    }


    I´m trying to sum the values from each property i have, and show the result in a specific textfield....I´m going to have multiple textfields...so you can see the amount of work i´ll have....There´s got to be a BETTER! way of doing this...I´m wide open to any advice.


    Regards
  • RickSDKRickSDK Posts: 1,237Registered Users @ @ @ @
    usually code might look more like this:

    [code]
    NSArray* objects = [contextApuramento executeFetchRequest:request error:&error];
    int sum=0;
    for(NSManagedObject *object in objects) {
    int value = [object valueForKey:@count] intValue];
    sum += value;
    }
    [/code]

    or something like that.
  • GHuebnerGHuebner Posts: 665Registered Users @ @ @
    edited June 2013
    I would watch the WWDC core data sessions online. They talk about what you want to do. You dont need to get a list of results and iterate through it. You can do it all in your core data fetch.
  • jsantojsanto Posts: 30New Users @
    Thanks guys, i really appreciate your help.
    regards
Sign In or Register to comment.