History in calculator (Swift)

Viewed 485

I have code for running the history in the application.

My app screenshot:

img

How can I improve it so that the numbers are displayed immediately when pressed (as shown in the video), and not just by pressing =.

gif

// Connected to button "="
@IBAction func equalitySignPressed(sender: UIButton) {
    if stillTyping {
        secondOperand = currentInput
    }
    dotIsPlaced = false

    addHistory(text: operationSign + displayResultLabel.text!)

    switch operationSign {
    case "+":
        operateWithTwoOperands{$0 + $1}
    case "-":
        operateWithTwoOperands{$0 - $1}
    case "×":
        operateWithTwoOperands{$0 * $1}
    case "÷":
        operateWithTwoOperands{$0 / $1}
    default: break
    }
}
func addHistory(text: String){
    //Add text
    resultLabelText.text =  resultLabelText.text! + "" + text
}
1 Answers

One option could be to define a separate variable for the label string that is constantly updated using calls to addHistory() after every UIButton press (number or operator), and then the updating of the label itself handled by didSet inside the variable definition:

var resultLabelString: String = "" {
      didSet {
          self.resultLabelText.text = self.resultLabelText.text! + "" + resultLabelString
      }
}

func addHistory(text: String){
    self.resultLabelString =  text
}
Related