Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9272689
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T15:55:52+00:00 2026-06-18T15:55:52+00:00

Using Core Data , I encountered a problem. I have an entity Movement with

  • 0

Using Core Data, I encountered a problem. I have an entity “Movement” with an attribute “amount”. How do I make the sum of all the “amount” of all instances? I’d like to understand how to use NSExpressionDescription, but it’s good enough NSSet.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T15:55:53+00:00Added an answer on June 18, 2026 at 3:55 pm

    Having a managedObjectContext:

    NSManagedObjectContext *managedObjectContext = ...
    

    We create a fetch request with return type dictionary:

    NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:NSStringFromClass([Movement class])];
    fetchRequest.resultType = NSDictionaryResultType;
    

    Then we create an expression description to calculate the sum:

    NSExpressionDescription *expressionDescription = [[NSExpressionDescription alloc] init];
    expressionDescription.name = @"sumOfAmounts";
    expressionDescription.expression = [NSExpression expressionForKeyPath:@"@sum.amount"];
    expressionDescription.expressionResultType = NSDecimalAttributeType;
    

    Set the request properties to fetch:

    fetchRequest.propertiesToFetch = @[expressionDescription];
    

    We could also set a predicate if we want.

    And last we execute the request and get an array containing a dictionary with one key(@”sumOfAmounts”) and its value is a NSNumber with the sum of amounts.

    NSError *error = nil;
    NSArray *result = [managedObjectContext executeFetchRequest:fetchRequest error:&error];
    if (result == nil)
    {
        NSLog(@"Error: %@", error);
    }
    else
    {
        NSNumber *sumOfAmounts = [[result objectAtIndex:0] objectForKey:@"sumOfAmounts"];
    }
    

    Cheers

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

im using core data i have an entity called questions whenever i add my
I'm using Core Data and I have a file-folder tree-like structure. So I've created
I'm using core data and I have a simple two-entity model. My navigation controller
I'm using core data and I have an entity defined called LogRecord. Elsewhere this
Using core data I'd like to fetch some data. My model uses some abstract
I'm using core data in my app. I have two entities that are related:
I have an App using core data with 3 entities with very similar attributes.
using Core Data, how would I list (i.e. return an NSArray of NSStrings) all
I have an app using core data framework. I was working fine. I just
I'm using Core data in an iOS project. I have the data model setup

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.