I am wondering if there are any nice Mathematical Parsers in Swift. That is, it can take a string and solve it. I need it to have factorials, exponents, square roots, and all the basic arithmetic operators. I want it to be built into Swift, not 3rd party(like DDMathParser and GCMathParser)It would be nice if you could a couple of them.
-
3Given that there aren't any Apple-provided frameworks for this, this would by necessity be a third-party solution. Things like DDMathParser can be used directly from Swift. – Brad Larson Jul 01 '15 at 18:23
-
DDMathParser is for Objective-C, I tried using it in swift, but whenever I run it, it always crashes even though I imported it. – Epic Defeater Jul 01 '15 at 18:31
-
While written in Objective-C, DDMathParser can indeed be used from Swift. Swift only had a problem with NSDecimal structs before 1.2, but that's been fixed in the language. Using it from Swift should not be a source of crashes, so you might want to examine the stack traces you're getting from those crashes to identify the reason behind them. – Brad Larson Jul 01 '15 at 18:40
-
Do you know how to use it, so you can help me? – Epic Defeater Jul 01 '15 at 18:42
-
Edit your question to include the code you wrote to try to use it, and the exact error messages you got. – rob mayoff Jul 01 '15 at 19:11
-
The above error indicates that the `-numberByEvaluatingString` method does not exist on NSString. From what I can see of Dave's code, this is a method added by the category here: https://github.com/davedelong/DDMathParser/blob/master/DDMathParser/NSString%2BDDMathParsing.h . Make sure that category is being compiled and recognized by your project, or you won't have that extension method on NSString. You can also try calling DDMathEvaluator directly, following the code in his category: https://github.com/davedelong/DDMathParser/blob/master/DDMathParser/NSString%2BDDMathParsing.m – Brad Larson Jul 06 '15 at 17:48
-
FYI DDMathParser was rewritten several months ago to be pure Swift. – Dave DeLong Jan 19 '16 at 16:41
1 Answers
There is no mathematical expression parser “built into Swift” in the way you mean. If you want to take strings provided by the user and parse them as mathematical expressions, you must either write your own parser, or use a third-party parser like DDMathParser
or swift-math-parser
.
Note that there is an expression evaluator included with the Foundation framework, which is part of iOS and Mac OS X and is accessible from both Objective-C and Swift. It's called NSExpression
.
Example:
let expression = NSExpression(format: "2+3*4")
print(expression.expressionValueWithObject(nil, context: nil))
// prints "14"
It doesn't include a factorial function, but this article describes how to add your own functions, and uses factorial as an example. You'll have to translate the example from Objective-C to Swift yourself.
The main problem with using NSExpression
is that, if the expression string has any errors, it simply crashes.
:; xcrun swift
Welcome to Apple Swift version 4.0.3 (swiftlang-900.0.74.1 clang-900.0.39.2). Type :help for assistance.
1> import Foundation
2> let ex = NSExpression(format: "1+", argumentArray: [])
Process 78868 stopped
* thread #1, queue = 'com.apple.main-thread', stop reason = internal ObjC exception breakpoint(-5).
frame #0: 0x0000000100000e90 repl_swift`repl_swift.repl_main() -> Swift.Int
repl_swift`repl_swift.repl_main() -> Swift.Int:
-> 0x100000e90 <+0>: pushq %rbp
0x100000e91 <+1>: movq %rsp, %rbp
0x100000e94 <+4>: xorl %eax, %eax
0x100000e96 <+6>: popq %rbp
Target 0: (repl_swift) stopped.
ex: NSExpression = <extracting data from value failed>
Execution stopped at breakpoint. Enter LLDB commands to investigate (type help for assistance.)
(lldb)
For this reason, it's not a good idea to call NSExpression
with strings provided by the user or any other external source.

- 375,296
- 67
- 796
- 848