swiftxcodecalculatoroperations

Calculator app does not get multiple-digit numbers and operations return zero at all times?


I'm using Swift 4 and the numbers are all in Int.

So I made a calculation class file:

import Foundation
import UIKit

class Calculation {

    var result = Int()
    var operationButton = String()
    var currentNumber = Int()

    func calculator() {

        switch operationButton {
        case "+":
            result = result + currentNumber
        case "-":
            result = result - currentNumber
        case "*":
            result = result * currentNumber
        case "/":
            result = result / currentNumber
        case "=":
            result = currentNumber
        default:
            break
        }

    }

        func textClear() {
            result = 0
            currentNumber = 0
            operationButton = "="
    }

And then I made 3 functions in UIControllerView for AC button, numbers button, and operations button to get the "0-9" and "-,+,*,/,=" directly from the button title.

    class ViewController: UIViewController {


    @IBOutlet weak var resultTextField: UITextField!

    var calculation = Calculation()

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        calculation.operationButton = "="
        resultTextField.text = "\(calculation.result)"
    }


    @IBAction func allClearButtonPressed(_ sender: UIButton) {
        calculation.textClear()
        resultTextField.text = "\(calculation.result)"
    }

    @IBAction func operationButtonPressed(_ sender: UIButton) {

        calculation.operationButton = sender.titleLabel!.text as String!
            calculation.operationButton = sender.titleLabel!.text!
        calculation.calculator()
        resultTextField.text = "\(calculation.result)"
    }

    @IBAction func numberButtonPressed(_ sender: UIButton) {

        calculation.currentNumber = Int((sender.titleLabel?.text)!)!
        let numberInput = String(calculation.currentNumber)
        resultTextField.text = numberInput
    }
}

First problem is that whenever I try to put a number e.g 21, it just returns 2, and then replaced by 1. The second problem would be the operators which always return zero to the "resultTextField".


Solution

  • Your numberButtonPressed function reads the text of the button, replaces the currentNumber and then assign it to the resultTextField.text. What you want is a function that adds the new digit to your current number. Inside your Calculator class.

    func pressedDigit(_ digit: Int) {
        currentNumber = currentNumber * 10 + digit
    }
    

    Then you can call this function and assign the currentNumber to your resultTextField.

    @IBAction func numberButtonPressed(_ sender: UIButton) {
        guard let digit = sender.titleLabel?.text?.flatMap({ Int(String($0)) }) else { return }
        calculation.pressedDigit(value: digit)
        resultTextField.text = "\(calculation.currentNumber)"
    }
    

    You also have to reset currentNumber after each calculation.