Unable to get the second value (var b) for calculation.
In this calculator program I got the first value (var a) from display.text and cleared the display.text.Then getting the second value (var b) for adding/Subtrating, the second value still assigned to var a and getting error
unexpectedly found nil for value b
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var display: UITextField!
@IBOutlet weak var nine: UIButton!
@IBOutlet weak var eight: UIButton!
@IBOutlet weak var seven: UIButton!
@IBOutlet weak var six: UIButton!
@IBOutlet weak var five: UIButton!
@IBOutlet weak var four: UIButton!
@IBOutlet weak var clear: UIButton!
@IBOutlet weak var result: UIButton!
@IBOutlet weak var add: UIButton!
@IBOutlet weak var subtract: UIButton!
@IBOutlet weak var multiply: UIButton!
@IBOutlet weak var three: UIButton!
@IBOutlet weak var two: UIButton!
@IBOutlet weak var one: UIButton!
var res : Int = 0
var a : Int = 0
var b : Int = 0
var actionBtn : Int = 0
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
@IBAction func numbers(_ sender: Any) {
display.text = display.text! + String((sender as AnyObject).tag)
print("displtxt = ", display.text)
}
@IBAction func functionality(_ sender: Any) {
actionBtn = (sender as AnyObject).tag
a = Int(display.text!)!
print("a = ", a)
display.text = ""
}
@IBAction func clearDisplay(_ sender: Any) {
display.text = ""
}
@IBAction func results(_ sender: Any) {
b = Int(display.text!)!
switch actionBtn {
case 10:
print("b = ", b)
res = a*b
print("res = ", res)
case 11:
res = a-b
case 12:
res = a+b
default:
display.text = ""
}
}
}