Мне надо разделить text field на label и получить ответ в другом label
Xcode все время выдает ошибку: "Unexpectedly found nil while implicitly unwrapping an Optional value" (отметил в коде "Fatal Error")
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) {
guard let adamValue = Int(adam.text!), adamValue != 0, // Fatal error
let totalAmountValue = Int(totalAmount.text!) else { return }
let result = totalAmountValue / adamValue
finish.text = String(result)
}
//Show calculated
// finish = totalAmount / adam
@IBOutlet weak var finish: UILabel!
"
Ответы (1 шт):
Автор решения: Oleg Soloviev
→ Ссылка
У вас в adam.text и в totalAmount.text нет значений, поэтому при их развертывании получается nil и exception. Чтобы исключить краш, попробуйте вот так:
guard let adamText = adam.text, let adamValue = Int(adamText), adamValue != 0,
let totalAmountText = totalAmount.text, let totalAmountValue = Int(totalAmountText) else { return }