1

I am writing a little app which I would like to use as a calculator. For the operators of the calculator I started with a switch statement to e.g. multiply two values. Therefore I created a dictionary and it worked fine.

I now found it would be quite useful and code saving if I'd write something like

number1 operationButton.currentTitle number2

where operationButton.currentTitle would represent +, -, * or /.

My question now is how can I convert the string of operationButton.currentTitle into a function call for these operators?

1
  • 1
    You should not rely on the button titles to have certain values (such as "+"). What if the title is changed to "add" later? – Better connect different actions to each button. Commented Apr 18, 2015 at 10:48

2 Answers 2

8

In Swift, you can treat functions like variables - for example, you can store them in dictionaries. Also, operators in Swift are just functions with non-alphabetic names (and that can be used infix etc.).

This means you can store binary operations in a dictionary:

let operators: [String:(Double,Double)->Double] = [
    "plus":     (+),
    "minus":    (-),
    "divide":   (/),
    "multiply": (*),
]

And then look up those operations and use whichever one you get, like so:

if let op = operators["divide"] {
    op(3,4)  // returns 0.75
}
Sign up to request clarification or add additional context in comments.

2 Comments

That's correct and nice – but in this concrete case, relying on the button titles (which may be different in different localizations) in order to determine which operation to perform seems a bad idea to me.
Then again, some hybrid of the two is possibly appropriate if you want to be able to configure what the buttons do at runtime
4

You should not rely on the button titles to have certain values in order to decide which operation to perform. What if + is changed to add later? And then localized to a different language? Or the button has no title at all, only a fancy image?

Better connect different actions to each button, such as

@IBAction func addAction(sender: UIButton) {
    // add operands ...
}

@IBAction func subtractAction(sender: UIButton) {
    // subtract operands ...
}

Note also that different operators require may require different error handling, e.g. the divideAction would check if the denominator is zero.

4 Comments

This is not an answer —a suggestion at best.
@VatsalManot: If you think so ... I think it shows a way towards a better solution of the problem.
@VatsalManot: It could well be that the question is a XY Problem, and if you read it as "how do I translate a button click into the math operation" then it is an answer.
Wow, had no idea what an XY problem was! Could you add a dummy edit? Because I can't change my vote.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.