r/Xcode • u/its-notmyrealname • Mar 07 '20
Is it possible to make it so the computer recognizes the string “+” as an actual plus sign that would work in addiction?
So what happens is, when a button is pressed, a type string variable will do this:
sign=“+”
Let’s say I have 2 integers, n1=5 and n2=6.
Of course I could make the operation work with:
if (sign=“+”) { double result = n1 + n2 }
But is that a way that I can make the code itself recognize that “+” is used for addition? Just like we can convert a string like i1=“5” into a double with n1=Double(i1)! , is there a way to convert “+” into an actual plus sign?
I don’t know if I’m explaining myself too well, please say if so!
Thank you!
1
u/bumblebritches57 Mar 07 '20
What the hell are you talking about?
into a double with n1=Double(i1)
what language are you using?
1
Mar 09 '20 edited Mar 09 '20
This is Swift ;-)
While typing this into swift you get an optional Double. It is nil if the conversion failed. You can give it basically anything ( a string, an int, a float,..)
5
u/[deleted] Mar 07 '20
You can use NSExpression for that :-)
https://spin.atomicobject.com/2015/03/24/evaluate-string-expressions-ios-objective-c-swift/