I am new to programming and am struggling to find a solution to a problem in the exercise I am using to learn Xcode programming. I have even copied the code from the book website I am using.
I can stop the fatal error if I add a question mark or a Exclamation mark to the following line:
This is how I alter the lines:
The code builds in both cases and only produces a Fatal Error when I run the simulator without the unwrapping tags.
With the unwrapping added the simulator runs, but when I enter the data and click on the link nothing appears in the MY BMI text box.
I have not mastered debugging yet so don't know how to discover where the fault lies.
Can anyone help as I can't learn any more Xcode until I can see where my mistakes are.
Here is the full code:
I can stop the fatal error if I add a question mark or a Exclamation mark to the following line:
Code Block self.weightInput.delegate = self self.heightInput.delegate = self
This is how I alter the lines:
Code Block self.weightInput!.delegate = self self.heightInput!.delegate = self
The code builds in both cases and only produces a Fatal Error when I run the simulator without the unwrapping tags.
With the unwrapping added the simulator runs, but when I enter the data and click on the link nothing appears in the MY BMI text box.
I have not mastered debugging yet so don't know how to discover where the fault lies.
Can anyone help as I can't learn any more Xcode until I can see where my mistakes are.
Here is the full code:
Code Block // ViewController.swift // BMI Calculator // // Created by Tony Hudson on 25/03/2021. import UIKit class ViewController: UIViewController, UITextFieldDelegate { @IBOutlet weak var weightInput: UITextField! @IBOutlet weak var heightInput: UITextField! @IBOutlet weak var BMIOutput: UITextField! @IBOutlet weak var categoryOutput: UILabel! override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. self.weightInput.delegate = self //added either ! or ? here and don't get fatal error self.heightInput.delegate = self //added either ! or ? here and don't get fatal error } @IBAction func calcBMI(_ sender: AnyObject) { if let heightStr = heightInput.text { if heightStr == "" { return } else { if let weightStr = weightInput.text { if weightStr == "" { return } else { if let heightNum = Double(heightStr) { if let weightNum = Double(weightStr) { let BMI: Double = (weightNum) / (heightNum * heightNum) BMIOutput.text = String(BMI) print(BMI) switch BMI { case 1..<15: categoryOutput!.text = "Very severely underweight" case 15...16: categoryOutput!.text = "Severely underweight" case 16..<18.5: categoryOutput!.text = "Underweight" case 18.5..<25: categoryOutput!.text = "Normal" case 25..<30: categoryOutput!.text = "Overweight" case 30..<35: categoryOutput!.text = "Moderately obese" case 35..<40: categoryOutput!.text = "Severely obese" case 40..<60: categoryOutput!.text = "Very severely obese" default: return } } } } } } } } func textFieldShouldReturn(_ textField: UITextField) -> Bool { self.weightInput.resignFirstResponder() self.heightInput.resignFirstResponder() return true } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } }