unexpectedly found nil while unwrapping an Optional value

XCODE BETA 7.4


So i am trying to make a tip calculator and i have 4 view controllers, one segue'd to the next all the way to the end. Iv written all of my code, and it would always crash when i press the last button which "calculates" the tip. So i rewrote my code and now im running it 3/4 of the way thru and it still crashes at the last button. here are my errors.


THE CONSOLE SAYS:

unexpectedly found nil while unwrapping an Optional value

(lldb)


and where it shows CPU, MEMORY, DISK, NETWORK, under that on THREAD 1 this is highlighted.


-> 0x30dd54 <+72>: trap <- Thread 1 : EXC_BREAKPOINT (code=EXC_ARM_BREAKPOINT, subcode=0xe7ffdefe)



and, heres my code.


import UIKit

class ViewController: UIViewController {

@IBOutlet var enteredAmount: UITextField!

@IBOutlet var enteredUsers: UITextField!

@IBOutlet var enteredTip: UITextField!

@IBOutlet var enteredAmountLabel: UILabel!

@IBOutlet var tipLabel: UILabel!

@IBOutlet var finalAmountLabel: UILabel!

@IBOutlet var tabPerPerson: UILabel!

@IBAction func clickForTotal(sender: AnyObject) {

let unwrappedAmountLabel = Int(enteredAmount.text!) ?? 0

let unwrappedTipLabel = Int(enteredTip.text!) ?? 0

let unwrappedUsers = Int(enteredUsers.text!) ?? 0

enteredAmountLabel.text = "\(unwrappedAmountLabel)"

tipLabel.text = "\(unwrappedTipLabel)"

tabPerPerson.text = "\(unwrappedUsers)"

}

override func viewDidLoad() {

super.viewDidLoad()

/

}

override func didReceiveMemoryWarning() {

super.didReceiveMemoryWarning()

/

}

}

UITextField's text property should be returning an empty string instead of nil if nothing has been entered, so the first thing you might want to check is that all of your IBOutlets are connected properly on the storyboard. If an outlet isn't connected it will be nil, and you will get that error when you try to interact with it.


If the debugger isn't showing you the line of code where the error is occuring, you could also narrow down the culprit by printing out info to the console from within your clickForTotal: action

if (enteredAmount == nil) {print("enteredAmount is not connected")}
if (enteredTip == nil) {print("enteredTip is not connected")}
// etc...


If all of the outlets are properly connected, then one of the UITextFields must be returning nil for it's text property.

let unwrappedAmountLabel = Int(enteredAmount.text ?? "0") ?? 0
// etc...

i tried making my 4-view controller app into a single view controller and the code ran just fine. I believe that each textfield from the first 3 segue's are not passing the information along to the final viewcontroller where my main code will run. I'm fairly new to Swift, so am i missing some sort of code or special instruction that passes the information entered on one view controller, down to the last view controller? or should i just be able to ctrl drag each UITextField from each view controller into my viewController.swift file and make it work?

The connections to the textfields won't automatically be transferred when you switch to another view controller (and the text field instances might go away completely if the view is no longer being used), so you will need to have some way of transferring the information to your other view controllers. The IBOutlets in each view controller are separate connections/pointers, even if they share the same name.


You could connect the input text fields to all the different view controllers that need access to them, but you still might run into trouble if the previous views are unloaded. This was more of a problem in older versions of iOS, but I think it is still important to be aware of.


I would probably create a seperate object in the nib/storyboard which is responsible for storing the information, and have each view controller connect to that object and update it with its part of the data.

unexpectedly found nil while unwrapping an Optional value
 
 
Q