title: CoreData Math Functions
body: <p>I've got a column with integer values in CoreData. While retrieving results from it, I want the column values to be subtracted with a number.</p>
  
  <p><strong>Something like</strong>: columnValue - someNumber (this number is entered by user)</p>
  
  <p>I know I may have to use NSPredicate for this, but am unaware if there's a function or syntax for it.</p>
  
  <p>The alternate right now I have is to iterate all column values and subtract with 'someNumber'. But I think there should be a better and efficient way to do this.</p>
  
  <hr>
  
  <p><strong>Edit: Code from @salo.dm 's answer</strong></p>
  
  <pre><code>       - (NSDictionary *)myFetchResults {
      //Predicate works fine
              NSPredicate *predicate = [NSCompoundPredicate andPredicateWithSubpredicates:[NSArray arrayWithObjects:pred1, pred2, nil]];
  
              /*Sort Descroptor - Sorting by 4 columns*/
              NSSortDescriptor *sortDesc1 = [NSSortDescriptor sortDescriptorWithKey:@"Column1" ascending:YES];
              NSSortDescriptor *sortDesc2 = [NSSortDescriptor sortDescriptorWithKey:@"Column2" ascending:YES];
              NSSortDescriptor *sortDesc3 = [NSSortDescriptor sortDescriptorWithKey:@"Column3" ascending:YES];
              NSSortDescriptor *sortDesc4 = [NSSortDescriptor sortDescriptorWithKey:@"Column4" ascending:YES];
  
              /*Get Data*/
              MyAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
              NSManagedObjectContext *context = [appDelegate managedObjectContext];
  
              NSEntityDescription *entity = [NSEntityDescription entityForName:@"TableName" inManagedObjectContext:context];
              NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
              [fetchRequest setEntity:entity];
              [fetchRequest setPredicate:predicate];
              [fetchRequest setSortDescriptors:[NSArray arrayWithObjects:sortDesc1, sortDesc2, sortDesc3, sortDesc4, nil]];
  
              NSArray *listData = [context executeFetchRequest:fetchRequest error:nil];
  
  
              /*Create subtract expression*/
              NSExpressionDescription *subExp1 = [[NSExpressionDescription alloc] init];
          [subExpLatitude setName:@"subtraction1"];
          [subExpLatitude setExpression:[NSExpression expressionForFunction:@"from:subtract:" 
                                                                  arguments:[NSArray arrayWithObjects:
                                                                             [NSExpression expressionForKeyPath:@"Column3"],
                                                                             [NSExpression expressionForConstantValue:[NSNumber numberWithDouble:someNumber1]],
                                                                             nil]]];
              [subExp1 setExpressionResultType:NSDoubleAttributeType];
  
              NSExpressionDescription *subExp2 = [[NSExpressionDescription alloc] init];
          [subExpLongitude setName:@"subtraction2"];
          [subExpLongitude setExpression:[NSExpression expressionForFunction:@"from:subtract:" 
                                                                      arguments:[NSArray arrayWithObjects:
                                                                                 [NSExpression expressionForKeyPath:@"Column4"],
                                                                                 [NSExpression expressionForConstantValue:[NSNumber numberWithDouble:someNumber2]],
                                                                                 nil]]];
              [subExp2 setExpressionResultType:NSDoubleAttributeType];
  
              /*Get difference data*/
              [fetchRequest setResultType:NSDictionaryResultType];
              [fetchRequest setPropertiesToFetch:[NSArray arrayWithObjects:subExp1, subExp2, nil]];
  
              NSArray *listDifference = [context executeFetchRequest:fetchRequest error:nil];
  
              NSLog(@"Subtraction 1: %@", [[listDifference objectAtIndex:0] objectForKey:@"subtraction1"]);
              NSLog(@"Subtraction 2: %@", [[listDifference objectAtIndex:0] objectForKey:@"subtraction2"]);
  
              NSMutableDictionary *dictResult;
              [dictResult setObject:listData forKey:@"Data"]
              [dictResult setObject:listDifference forKey:@"Difference"]
  
        return dictResult;
  }
  </code></pre>
  
  <hr>
  
  <p><strong>Edit: Get coredata object</strong></p>
  
  <p>This doesn't work.</p>
  
  <pre><code>NSExpressionDescription *expEntity = [[NSExpressionDescription alloc] init];
  [expEntity setName:@"TableNameEntity"];
  [expEntity setExpression:[NSExpression expressionForKeyPath:@"objectID"]];      //Searches for a column for the name specified
  [expEntity setExpressionResultType:NSObjectIDAttributeType];}
  </code></pre>
  
  <p>Had to change it to below to get it working (Assuming this is the correct way)</p>
  
  <pre><code>    NSExpressionDescription *expEntity = [[NSExpressionDescription alloc] init];
      [expEntity setName:@"TableNameEntity"];
      [expEntity setExpression:[NSExpression expressionForEvaluatedObject]];
      [expEntity setExpressionResultType:NSObjectIDAttributeType];
  </code></pre>
  
  <p>I added expEntity to the setPropertiesToFetch list. Now I get two values in the dictionary.</p>
  
  <pre><code>{
      TableNameEntity = "0x5e22120 &lt;x-coredata://1A659A52-9321-4ACD-992B-04F20E7BDCED/TableNameEntity/p1640&gt;";
      subtractionValue = "-24.13";
  }
  </code></pre>
  
  <p>When I try to retrieve and access TableNameEntity from the dictionary, the app crashes.</p>
  
  <pre><code>TableNameEntity *tableEntity = (TableNameEntity *)[dict objectForKey:@"TableNameEntity"];
  tableEntity.column1 //This is not the exact code. But this operation crashes with error
  
  *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[_NSObjectID_48_0 column1]: unrecognized selector sent to instance 0x5e22120'
  </code></pre>
  
  <p>Here if you notice, the value for key TableNameEntity is contained in quotes, so I guess its being returned as a string.</p>
  
  <p>See if you can correct what I've done wrong.</p>
  
  <hr>
  
  <p>I've tried an alternate to get columns values in the dictionary. Here it is (this works fine). But I guess its not a good approach.</p>
  
  <pre><code>[fetchRequest setPropertiesToFetch:[NSArray arrayWithObjects:subExp1, @"column1", @"column2", ... @"columnN" nil]];
  </code></pre>