如何将结果从文本字段划分到标签并将其放在另一个标签上

How divide result from text field to label and get it on another label

我需要完成 (Label) = totalAmount(TextField) / adam (Label)。 这是我的代码:

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!

在我看来,您只是在访问局部变量,而不是 equal() 函数中的文本字段。

最好为局部变量和 class 属性使用不同的名称以避免混淆。

所以首先让我们验证文本字段包含可以转换为 Int 变量的值,否则 return 直接使用 guard。然后这只是一个除以值的问题,但当然我们也想避免由零决定,所以我们检查

@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)
}

通过使用整数,我们进行整数除法并因此失去精度,如果你想保持精度,你可以在上面的代码中切换到 Double 并可能添加一些结果格式

@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)
}

我不确定你的 finish 标签是什么,因为你没有标签,但鉴于你打算输出你的答案:

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"
}