if let statement of Storyboard is not working properly

Viewed 27

I am learning Swift with Storyboard, and making a Celsius to Fahrenheit calculator.

I am trying to make the text fields to accept only number. When I try converting with number, it works. But when I try with letters, it gives me error saying: Thread 1: "-[UIView text]: unrecognized selector sent to instance 0x125f09760"

Something is wrong when else{} is compiled, but I don't know why it happens.

Storyboard

enter image description here

ViewController

//
//  ViewController.swift
//  TemplatureConverter
//
//  Created by kawa on 2022-09-22.
//

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var celsiusInput: UITextField!
    @IBOutlet weak var fahrenheitInput: UITextField!
    @IBOutlet var error_message: UILabel!
    
        var celsius:Float = 0
    var fahrenheit:Float = 0
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
    }

    @IBAction func to_F(_ sender: Any) {
        if let i = Float(celsiusInput.text!){
            celsius = i
            fahrenheit = (celsius * 1.8) + 32
            fahrenheitInput.text! = String(fahrenheit)
        }else{
            celsius = 0
            fahrenheit = 0
            error_message.text! = "Please type number!"
        }
    }
    
    
    @IBAction func to_C(_ sender: Any) {
        if let i = Float(fahrenheitInput.text!){
            fahrenheit = i
            celsius = (fahrenheit - 32) * 0.5556
            celsiusInput.text = String(celsius)
        }else{
            fahrenheit = 0
            celsius = 0
            error_message.text! = "Please type number!"
        }
    }
}




0 Answers
Related