Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import UIKit
- class ViewController: UIViewController {
- @IBOutlet weak var number1TextField: UITextField!
- @IBOutlet weak var number2TextField: UITextField!
- @IBOutlet weak var resultLabel: UILabel!
- override func viewDidLoad() {
- super.viewDidLoad()
- }
- @IBAction func calculateButtonTapped(_ sender: UIButton) {
- guard let number1Text = number1TextField.text, let number2Text = number2TextField.text else {
- return
- }
- guard let number1 = Int(number1Text), let number2 = Int(number2Text) else {
- resultLabel.text = "Invalid input"
- return
- }
- var result: Int?
- switch sender.tag {
- case 0: // Addition
- result = number1 + number2
- case 1: // Subtraction
- result = number1 - number2
- case 2: // Multiplication
- result = number1 * number2
- case 3: // Division
- if number2 != 0 {
- result = number1 / number2
- } else {
- resultLabel.text = "Cannot divide by zero"
- }
- default:
- break
- }
- if let resultValue = result {
- resultLabel.text = String(resultValue)
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement