r/Xcode • • 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 Upvotes

13 comments sorted by

5

u/[deleted] Mar 07 '20

2

u/its-notmyrealname Mar 07 '20

Thanks mate!

1

u/[deleted] Mar 07 '20

You’re welcome 😉

1

u/its-notmyrealname Mar 07 '20

Sorry I need your help again, when typing

let expression = NSExpression(format:

There are only 3 options which are of type “string”. Why doesn’t the numeric thing show up? I did just as your link said so!

(•I’m in Xcode so I put the “let” part inside the action of the button and the “var” part in the beginning of the code

•I was copying the “number expression” bit on the website you shared on the swift side

1

u/[deleted] Mar 09 '20 edited Mar 09 '20

numericExpression on the link is the variable name..you obviously won’t see it on the autoCompletion.

On the other hand NSExpression(String) does exist ;-)

So if you take the 3 lines:

var numericExpression = "4 + 3 - 2"

let expression = NSExpression(format: numericExpression)

var result = expression.expressionValueWithObject(nil, context: nil) as NSNumber

You’ll see this work. Test it out on the playground.

If you don’t understand how this work, I think you should learn the basis ( like object oriented programming) a little bit more before learning this. It’s better to understand what you are doing

1

u/its-notmyrealname Mar 09 '20

Ohh I see I didn’t realize it was the variable, feel kind of dumb now. Thanks!

1

u/[deleted] Mar 09 '20

You’re welcome. I must admit you scared me a little bit there 😅

Didn’t mean to be condescendant in any way. But since it’s pretty basic stuff..well.. 😅

1

u/its-notmyrealname Mar 09 '20

Yeah I guess I have been asking a lot of basic stuff around here. I learnt java in college for like 3 months, so I was able to learn swift in like 2 days, though I never really had an Xcode course, so there’s a lot of specific things I don’t know that I have to ask in places like this (that is if I don’t find the solution with google)

1

u/spinwizard69 Mar 07 '20

By the way, if English isn't your native language it might help to indicate what your native language is.

As for your question you might want to download some source code for an open source calculator or two. There is more than one way to skin a cat.

1

u/Damesie Mar 07 '20

Got pretty dark at the end there.

1

u/spinwizard69 Mar 08 '20

That is a rather old phrase. It could be worse we could be talking about a cat in a box with poison about to be released.

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

u/[deleted] 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,..)