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

  • Home
  • SEARCH
  • 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 7532485
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T05:28:04+00:00 2026-05-30T05:28:04+00:00

Task: I am planning to parse a formula string in NSPredicate and to replace

  • 0

Task:

I am planning to parse a formula string in NSPredicate and to replace variables in the string by their numeric values. The variables are names for properties of existing object instances in my data model, for instance I have a class “company” with an instance “Apple Corp.”

Set-up:

My formula would like look like this: “Profitability_2011_in% = [Profit 2011] / [Revenue 2011]”

The instance “Apple Corp” would have the following properties:

Revenue 2009 = 10, Revenue 2010 = 20, Revenue 2011 = 30,
Profit 2009 = 5, Profit 2010 = 10, Profit 2011 = 20.

Hence, the formula would yield 20 / 30 = 67%.

Variables are usually two-dimensional, for instance defined by “profit” as the financial statement item and “year” (for instance 2011).
The variables are enclosed in [ ] and the dimensions are separated by ” ” (whitespace).

How I would do it

My implementation would begin with NSRegularExpression’s matchesInString:options:range: to get an array of all variables in the formula (Profit 2011, Revenue 2011) and then construct an NSDictionary (key = variable name) out of this array by querying my data model.

What do you think?

  • Is there a better way to do it in your view?
  • In the formula, how would you replace the variables by their values?
  • How would you parse the formula?

Thank you!!

  • 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-05-30T05:28:05+00:00Added an answer on May 30, 2026 at 5:28 am

    Yes, you can do this. This falls under the category of “Using NSPredicate for things for which it was not intended”, but will work just fine.

    You’ll need to replace your variables with a single word that start with a $, since that’s how NSPredicate denotes variables:

    NSPredicate *p = [NSPredicate predicateWithFormat:@"foo = $bar"];
    

    However you want to do that, great. NSRegularExpression is a fine way to do that.

    Once you do that, you’ll have something like this:

    @"$profitability2011 = $profit2011 / $revenue2011"
    

    You can then pop this through +predicateWithFormat:. You’ll get back an NSComparisonPredicate. The -leftExpression will be of type NSVariableExpressionType, and the -rightExpression will be of type NSFunctionExpressionType.

    This is where things start to get hairy. If you were to -evaluteWithObject:substitutionVariables:, you’d simply get back a YES or NO value, since a predicate is simply a statement that evaluates to true or false. I haven’t explored how you could just evaluate one side (in this case, the -rightExpression), but it’s possible that -[NSExpression expressionValueWithObject:context:] might help you. I don’t know, because I’m not sure what that “context” parameter is for. It doesn’t seem like it’s a substitution dictionary, but I could be wrong.

    So if that doesn’t work (and I have no idea if it will or not), you could use my parser: DDMathParser. It has a parser, similar to NSPredicate‘s parser, but is specifically tuned for parsing and evaluating mathematical expressions. In your case, you’d do:

    #import "DDMathParser.h"
    
    NSString *s = @"$profit2011 / $revenue2011";
    NSDictionary *values = ...; // the values of the variables
    NSNumber *profitability = [s numberByEvaluatingStringWithSubstitutions:values];
    

    The documentation for DDMathParser is quite extensive, and it can do quite a bit.


    edit Dynamic variable resolution

    I just pushed a change that allows DDMathParser to resolve functions dynamically. It’s important to understand that a function is different from a variable. A function is evaluated, whereas a variable is simply substituted. However, the change only does dynamic resolution for functions, not variables. That’s ok, because DDMathParser has this neat thing called argumentless functions.

    An argumentless function is a function name that’s not followed by an opening parenthesis. For convenience, it’s inserted for you. This means that @"pi" is correctly parsed as @"pi()" (since the constant for π is implemented as a function).

    In your case, you can do this:

    Instead of regexing your string to make variables, simply use the names of the terms:

    @"profit_2011 / revenue_2011";
    

    This will be parsed as if you had entered:

    @"divide(profit_2011(), revenue_2011())"
    

    You can the set up your DDMathEvaluator object with a function resolver. There are two examples of this in the DDMathParser repository:

    1. This example shows how to use the resolver function to look up the “missing” function in a substitution dictionary (this would be most like what you want)
    2. This example shows you to interpret any missing function as if it evaluated to 42.

    Once you implement a resolver function, you can forego having to package all your variables up into a dictionary.

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

Sidebar

Related Questions

TASK : I have an existing xml document (UTF-8) which uses xml namespaces and
I am in the planning stages for a task server development project. I would
I have been given the task of coding the form below. I was planning
I'm currently planning an application that does work on an object relational database. The
Task: implement paging of database records suitable for different RDBMS. Method should work for
Task at hand — I have three versions of some code, developed by different
Task: I have a camera mounted on the end of our assembly line, which
The task is to take a list of tables which is changeable. Write a
The task is simple, and the answers might be many. But here goes: On
Simple task like make AJAX request , pass one parameter and return result, can

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.