Hallo,
Ich habe ein einfaches Konvertierungsprogramm geschrieben. Alle Eingaben funktionieren, nur wenn der user in das kg Feld etwas hineinschreibt und es in lbs konvertiert, dann gibt es einen crash mit der Fehlermeldung: Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value. Ich freue mich sehr ueber Rueckmeldung, um sicherzugehen, dass ich das Thema Optionals noch mehr durchdringe. Anbei code und interface
Alles anzeigen
Ich habe ein einfaches Konvertierungsprogramm geschrieben. Alle Eingaben funktionieren, nur wenn der user in das kg Feld etwas hineinschreibt und es in lbs konvertiert, dann gibt es einen crash mit der Fehlermeldung: Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value. Ich freue mich sehr ueber Rueckmeldung, um sicherzugehen, dass ich das Thema Optionals noch mehr durchdringe. Anbei code und interface
Quellcode
- class ConversionVC: UIViewController, UITextFieldDelegate {
- @IBOutlet weak var stackDistance: UIStackView!
- @IBOutlet weak var stackWeight: UIStackView!
- @IBOutlet weak var stackLIquid: UIStackView!
- @IBOutlet weak var lbs: UITextField!
- @IBOutlet weak var kg: UITextField!
- @IBOutlet weak var liter: UITextField!
- @IBOutlet weak var gallons: UITextField!
- @IBOutlet weak var km: UITextField!
- @IBOutlet weak var miles: UITextField!
- var status = 0
- var unitInput: Double = 0
- var result: Double = 0
- // var factor: Double = 0
- @IBOutlet weak var selectMeasurement: UISegmentedControl!
- override func viewDidLoad() {
- super.viewDidLoad()
- stackWeight.isHidden = false
- stackDistance.isHidden = true
- stackLIquid.isHidden = true
- lbs.delegate = self
- kg.delegate = self
- liter.delegate = self
- gallons.delegate = self
- km.delegate = self
- miles.delegate = self
- }
- func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
- let allowedCharacters = ".0123456789"
- let allowedCharacterSet = CharacterSet(charactersIn: allowedCharacters)
- let typedCharacterSet = CharacterSet(charactersIn: string)
- return allowedCharacterSet.isSuperset(of: typedCharacterSet)
- }
- @IBAction func reset(_ sender: Any) {
- lbs.text = ""
- kg.text = ""
- liter.text = ""
- gallons.text = ""
- km.text = ""
- miles.text = ""
- }
- override func didReceiveMemoryWarning() {
- super.didReceiveMemoryWarning()
- }
- @IBAction func convert(_ sender: Any)
- {
- if stackWeight.isHidden == false
- {
- if lbs.text == " " && kg.text != " "
- {
- unitInput = Double(kg.text!)!
- lbs.text = String(calculateMulti(factor: 2.2))
- }
- else
- {
- unitInput = Double(lbs.text!)!
- kg.text = String(calculateDivi(factor: 2.2))
- }
- }
- if stackLIquid.isHidden == false
- {
- if liter.text == "" && gallons.text != ""
- {
- unitInput = Double(gallons.text!)!
- liter.text = String(calculateMulti(factor: 3.78))
- }
- else
- {
- unitInput = Double(liter.text!)!
- gallons.text = String(calculateDivi(factor: 3.78))
- }
- }
- if stackDistance.isHidden == false
- {
- if miles.text == "" && km.text != ""
- {
- unitInput = Double(km.text!)!
- miles.text = String(calculateDivi(factor: 1.6))
- }
- else
- {
- unitInput = Double(miles.text!)!
- km.text = String(calculateMulti(factor: 1.6))
- }
- }
- }
- @IBAction func indexChanged(_ sender: Any) {
- switch selectMeasurement.selectedSegmentIndex{
- case 0: stackWeight.isHidden = false
- stackDistance.isHidden = true
- stackLIquid.isHidden = true
- print("selected 0")
- case 1:
- stackDistance.isHidden = false
- stackWeight.isHidden = true
- stackLIquid.isHidden = true
- print ("selected 1")
- case 2: stackLIquid.isHidden = false
- stackDistance.isHidden = true
- stackWeight.isHidden = true
- print ("selected 2")
- default: break
- }
- }
- func calculateMulti(factor: Double)-> Double
- {
- result = unitInput * factor
- return result
- }
- func calculateDivi(factor: Double)-> Double
- {
- result = unitInput/factor
- return result
- }
- }