Realtime Arithmetic Evaluation in iOS

nish abe
2 min readApr 13, 2020

Let me take a wild guess.

Once you got past the classic ‘Hello World!’, we all have done a project called ‘The Calculator’, regardless of the front end tech stack.

Recently, I came to know about a feature in an expense tracking app called “Spend Together”. When you add an expense, it keeps evaluating the operations on operands in realtime.

Here is a quick look at that feature:

At first glance, nothing fancy, or so I thought.

The more I thought about it, the complexity began to unravel. I spend a weekend doing some research, refreshing the arithmetics, evaluating different options. And thus I discovered the powerful API called ‘NSExpression’. The most important aspect of NSExpression is that it keeps the evaluating going until your input is correct. If you give an incorrect input expression like ‘1+’, it will just crash on you.

At the end of two days, I came up with a working prototype that mimics this feature. Overall, time well spent with lots of great learning.

Here is a quick look:

Please feel free to check the code here.

--

--

nish abe

"Curious to know how things work and interested in making things work". https://nishabe.github.io/