0

I am using NSExpression to solve strings from user input. Here is the code

var userInput = "5+"
var solveUserInput = NSExpression(format: "\(userInput)")

It will crash my program because it can't solve it, but what I want to do is to return nil if the string is unsolvable by NSExpression. Is there any way to do this?

Epic Defeater
  • 2,107
  • 17
  • 20
  • 1
    Look [here](http://stackoverflow.com/questions/28005595/handle-nsinvalidargumentexception-exception-from-nsexpression-with-swift?lq=1) and [here](http://stackoverflow.com/questions/24710424/catch-an-exception-for-invalid-user-input-in-swift). – Mario Zannone Jul 09 '15 at 08:54

1 Answers1

1

NSExpression is not really meant for that kind of usage (evaluation of user input).

I suggest that you have a look at DDMathParser instead.

Mario Zannone
  • 2,843
  • 12
  • 18