Post

Replies

Boosts

Views

Activity

Reply to Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value
Look at this: 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 } Line 2 you declare an IBOutlet, as implicitly unwrap. That means that weightInput is an optional, so either nil or some value. Implicitly unwrap means that if you call it once it has a value, you don't need to unwrap anymore. It is done implicitly. But, if you call when it is not initialized (nil) and try to unwrap, of course you crash. And an IBOutlet is loaded and initialized when the view is loaded. But the IBOulet var is initialized only if it is properly connected to the object in storyboard. Otherwise, no way to know which var to initilize, hence it remains nil. So most likely in your case, the connection between th IBOutlet and the TextField in storyboard is not set or is broken. Then, when you call line 10: self.weightInput.delegate = self Do you see a black dot on the left of @IBOutlet weak var weightInput: UITextField! If not, control drag from the white circle to the textField in storyboad. The connection will be made, and crash will disappear (if you do it for all IBOutlets). Note: you wrote: 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. It is logical not to crash with ?, but a bit surprising you do not crash with ! If weightInput is nil, when you set with optional chaining self.weightInput?.delegate = self in fact it does nothing (execution "halted" after ? because of nil). TextField appear on screen, you can type, but delegate functions (as textFieldShouldReturn) are not called. Nothing appears in the MY BMI text
Topic: Programming Languages SubTopic: Swift Tags:
Mar ’21
Reply to Run something before another thing
I need the function to run before it sets the text. Which function ? You don't show anything about it. How do you want anyone to guess ? Please, explain properly what you need and provide relevant information, otherwise, it is impossible to help.
Topic: UI Frameworks SubTopic: UIKit Tags:
Mar ’21
Reply to JSON Core Data favorites
If I understand your question correctly. You have 2 things : the data for all the information you save in Core Data the status of some as favorite. This may be stored in the core data or simpler in suerSettings (with a reference to the data ID)
Topic: Programming Languages SubTopic: Swift Tags:
Mar ’21
Reply to Pass Data Forward Tips
That's not totally clear… without pushing my initial viewcontroller? What do you mean ? What is the problem with push ? Did you consider using a modal segue for instance of a popover ? Or embedding your first VC in a navigation controller ?
Topic: Programming Languages SubTopic: Swift Tags:
Mar ’21
Reply to Append to Double
Yes it is, but not this way. You need to convert first to String, then append then convert back to Double. Such as: var num: Double = 1 let numString = String(num) let numStrPlus = "1" + numString var num2 = Double(numStrPlus)! // num.append(1) print(num2) You can have a more condensed form: var num: Double = 1 var num2 = Double("1" + String(num)) ?? 0 or var num2 = Double(String(1) + String(num)) ?? 0 If you really want to write it as append, you can define an extension to Double: extension Double { func append(_ val: Int) - Double { return Double(String(val) + String(self)) ?? 0 } } num2 = num.append(1)
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Mar ’21