Tim Tim - 4 years ago 118
iOS Question

Swift calculator. "0" remains on the screen



In the code is one minor problem which I'm trying to solve but have not yet found the solution.

This code is for the calculator.

It is doing right calculations.

But when I press "clear" button, in front of new calculation remains "0".

For example: I press "clear" and after "2". On the screen will appear "02".

I would be very grateful if someone would look at the code and see the solution.

import UIKit
import AVFoundation

class ViewController: UIViewController {


@IBOutlet weak var outputLbl: UILabel!
var btnSound: AVAudioPlayer!

@IBAction func clearBtnPressed(_ sender: Any) {
playSound()
runningNumber = ""
leftValStr = ""
outputLbl.text = ""
currentOperation = Operation.Empty
}

enum Operation: String {
case Divide = "/"
case Multiply = "*"
case Subtract = "-"
case Add = "+"
case Empty = "Empty"
}

var currentOperation = Operation.Empty
var runningNumber = ""
var leftValStr = ""
var rightValStr = ""
var result = ""


override func viewDidLoad() {
super.viewDidLoad()

let path = Bundle.main.path(forResource: "btn", ofType: "wav")
let soundURL = URL(fileURLWithPath: path!)

do {
try btnSound = AVAudioPlayer(contentsOf: soundURL)
btnSound.prepareToPlay()
} catch let err as NSError {
print(err.debugDescription)
}
outputLbl.text = ""
}


@IBAction func numberPressed(sender: UIButton) {
playSound()
runningNumber += "\(sender.tag)"
outputLbl.text = runningNumber

}

@IBAction func onDividePressed(sender: AnyObject) {
processOperation(operation: .Divide)
}

@IBAction func onMultiplyPressed(sender: AnyObject) {
processOperation(operation: .Multiply)
}

@IBAction func onSubtractPressed(sender: AnyObject) {
processOperation(operation: .Subtract)
}

@IBAction func onAddPressed(sender: AnyObject) {
processOperation(operation: .Add)
}

@IBAction func onEqualPressed(sender: AnyObject) {
processOperation(operation: currentOperation)
}


func playSound() {
if btnSound.isPlaying {
btnSound.stop()
}
btnSound.play()
}

func processOperation(operation: Operation) {
playSound()
if currentOperation != Operation.Empty {
if runningNumber != "" {
rightValStr = runningNumber
runningNumber = ""

if currentOperation == Operation.Multiply {
result = "\(Double(leftValStr)! * Double(rightValStr)!)"
} else if currentOperation == Operation.Divide {
result = "\(Double(leftValStr)! / Double(rightValStr)!)"
} else if currentOperation == Operation.Subtract {
result = "\(Double(leftValStr)! - Double(rightValStr)!)"
} else if currentOperation == Operation.Add {
result = "\(Double(leftValStr)! + Double(rightValStr)!)"
}

leftValStr = result
outputLbl.text = result
}

currentOperation = operation
} else {
leftValStr = runningNumber
runningNumber = ""
currentOperation = operation
}
}
}

Tim Tim
Answer Source

0 came from Operation.Empty
I solved this problem in the following way:
if (runningNumber == "0") { runningNumber.remove(at: runningNumber.startIndex) }
I'm not sure that this solution is optimal but now the calculator works perfect
I thank everyone who took the time to help. Thank you, guys!

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download