skip to Main Content

I need to finish(Label) = totalAmount(TextField) / adam (Label).
Here is my code:

import UIKit
var plusAndMinus = 0

class ViewController234: UIViewController {

@IBOutlet weak var adam: UILabel!

@IBAction func plus(_ sender: Any) {
    plusAndMinus += 1
   adam.text = "(plusAndMinus)"
}

@IBAction func minus(_ sender: Any) {
    if(plusAndMinus != 0 ) {
        plusAndMinus -= 1
    }
   adam.text = "(plusAndMinus)"
}

// Write total amonunt to calculate

@IBOutlet weak var totalAmount: UITextField!

@IBAction func equal(_ sender: Any) {
  
    var adam = 0
    var totalAmount = 0
    var result = 0
    adam = Int(adam)
    totalAmount = Int(totalAmount)
    guard result != 0 else {return}
   
    result = totalAmount / adam
    finish.text = String(result)
}

//Show calculated
// finish = totalAmount / adam

@IBOutlet weak var finish: UILabel!

2

Answers


  1. It looks to me like you are only accessing your local variables and not the text fields in the equal() function.

    It is better to use different names for local variables and class properties to avoid confusion.

    So first lets validate that the text fields contains values that can be converted to Int variables and otherwise return directly by using guard. Then it’s simply a matter of dividing the values but of course we also want to avoid decision by zero so we check that to

    @IBAction func equal(_ sender: Any) {
        guard let text = Adam.text, let adamValue = Int(text), adamValue != 0,
              let totalAmountValue = Int(totalAmount.text!) else { return }
    
        let result = totalAmountValue / adamValue
        finish.text = String(result)
    }
    

    By using integers we are doing integer division and thus loosing precision, if you want to keep the precision you can switch to Double in the code above and perhaps add some formatting of the result

    @IBAction func equal(_ sender: Any) {
        guard let text = Adam.text, let adamValue = Double(text), adamValue != 0,
              let totalAmountValue = Double(totalAmount.text!) else { return }
    
        let result = totalAmountValue / adamValue
        finish.text = String(format: "%.2f", result)
    }
    
    Login or Signup to reply.
  2. I’m not sure what your finish label is because you don’t have a label for it, but given that you’re meaning to output your answer to it:

    enum InputError: Error {
        case emptyAdamLabel
        case emptyTotalAmountLabel
        case divideByZero
        case invalidFormat
    }
    
    func calculate() throws {
        if let adamText = adam.text, adamText.trimmingCharacters(in: .whitespacesAndNewlines) == "" {
            throw InputError.emptyAdamLabel
        } else if let total = totalAmount.text, total.trimmingCharacters(in: .whitespacesAndNewlines) == "" {
            throw InputError.emptyTotalAmountLabel
        } else if let adamText = adam.text, adamText == "0" {
            throw InputError.divideByZero
        } else if let adamNumber = Double(adam.text!), let totalAmountNumber = Double(totalAmount.text!) {
            finish.text = String(format: "%.2f", totalAmountNumber / adamNumber)
        } else {
            throw InputError.invalidFormat
        }
    }
    
    @IBAction func equal(_ sender: Any) {
        do {
            try calculate()
        } catch InputError.emptyAdamLabel {
            print("Adam's label cannot be empty")
        } catch InputError.emptyTotalAmountLabel {
            print("Total Amount's label cannot be empty")
        } catch InputError.divideByZero {
            print("Adam's label cannot be zero")
        } catch InputError.invalidFormat {
            print("Both Adam's label and Total Amount's label have to be integers")
        } catch {
            print("Unknown errors")
        }
        adam.text = "0"
        totalAmount.text = "0"
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search