davedelong / DDMathParser

String → Number
MIT License
856 stars 153 forks source link

swift ios app - string.numberByEvaluatingString() #108

Closed j259liu closed 9 years ago

j259liu commented 9 years ago

Hi,

I am new to swift ios programing and I have a issue using numberByEvaluatingString(). Basically, if I input something like "ln3" without the bracket or like "3++++3", the app will crash... Is there a way to prevent the app from crashing if wrong input is entered? Thanks!

davedelong commented 9 years ago

If your app is crashing, then you're not using the API correctly. Without seeing your code or see the crash log, I can't tell you why.

However, both the 1.1 and the 2.0 branch parse both of those examples correctly.

j259liu commented 9 years ago

Thanks for replying! My code is simply the following: let result = inputText.text.numberByEvaluatingString().doublevalue where inputText is a textview.

The error message is the following (when evaluating "9+++"): error: Error Domain=com.davedelong.ddmathparser Code=10 "no right operand to binary +" UserInfo={NSLocalizedDescription=no right operand to binary +} fatal error: unexpectedly found nil while unwrapping an Optional value (lldb)

The code will crash the app if wrong format is being entered such as "ln(5", "3++++1", "2+", etc... I just need the app not crashing when wrong format is entered. Is there something like "On Error Resume/Goto" stuff in Swift?

j259liu commented 9 years ago

After reviewing your Unit Test for Issue 108, I think I found a way around it... Can I just put guard in front of my code? guard let result = inputText.text.numberByEvaluatingString().doublevalue else {return} in this way, the app seems not exiting...

davedelong commented 9 years ago

Hm, interesting; I'm not sure what the issue would be, and since this is the Objective-C code, it's not worth my time to investigate it.

Instead of using master and the Objective-C code, I would recommend using the 2.0 branch, which contains the entire parser re-written in Swift, and packaged as a framework.