Title: Fatal Error unexpectedly found nil while unwrapping an optional value [Firebase]
Tags: ios;firebase;swift3;firebase-authentication;option-type
Question: I have looked all over for an answer to my problem but none of the solutions fit. I think its a problem with firebase as I have used breakpoints to see exactly when and where the error was occuring. Everytime it happened when the firebase method ran.
With firebase I am trying to sign a user up using the createUser method. The problem is every time I run this method my app crashes and prints this to the console. I have already checked to make sure my outlets were connected and they werent. However when I connected them I still recieved the problem. What makes this more interesting though is when I print the contents of the variables email and password, it prints out. Here is my code:
import UIKit
import Firebase
class RegisterVC: UIViewController, UITextFieldDelegate {
@IBOutlet weak var fName: UITextField!
@IBOutlet weak var LName: UITextField!
@IBOutlet weak var Username: UITextField!
@IBOutlet weak var Email: UITextField!
@IBOutlet weak var Password: UITextField!
@IBOutlet weak var confirmPassword: UITextField!
let firebaseAuth = Auth.auth()
//when sign up is tapped
@IBAction func signUpTapped(_ sender: Any) {
signUpAction()
}
var ref: DatabaseReference!
override func viewDidLoad() {
super.viewDidLoad()
ref = Database.database().reference()
self.fName.delegate = self
self.LName.delegate = self
self.Username.delegate = self
self.Email.delegate = self
self.Password.delegate = self
self.confirmPassword.delegate = self
}
//when return button is pressed ... what should happen
func textFieldShouldReturn(_ textField: UITextField) -&gt; Bool {
fName.resignFirstResponder()
LName.resignFirstResponder()
Username.resignFirstResponder()
Email.resignFirstResponder()
Password.resignFirstResponder()
confirmPassword.resignFirstResponder()
return true
//hides keyboard if you touch view
override func touchesBegan(_ touches: Set<UITouch&gt;, with event: UIEvent?) {
self.view.endEditing(true)
}
//function is called from sign up button tapped function
func signUpAction(){
guard let email = Email.text, let password = Password.text else{
print("Something is Happenening")
return}
print(email)
print(password)
//adds user under the authentication tab on firebase
firebaseAuth.createUser(withEmail: email, password: password) { (user, err) in
if err != nil {
print(err!)
print(email)
print(password)
return  }
else{
self.createProfile(user!)
self.performSegue(withIdentifier: "signUpToMainScene", sender: nil) }
My indentation may look off as this is practically my first time posting on stack overflow. These are a list of things I have tried:
Changed parameters to regular strings
Tried replacing email Email.text! and same with password
reconnecting outlets however I dont think thats the problem because I can still print the variables
updated pods to see if there was a new version of firebase
Error: Fatal Error unexpectedly found nil while unwrapping an optional value
Please help me this problem, also I am still relatively new to swift and xcode so please try and explain things as I am still learning a lot of new things. Also ignore the comments, they are there to help me remember what is going on with each function and specific statements.
My actual code does contain the createProfile function, I just didnt add in this because I thought it was irrelavant
Comment: no it does contain the function I just didnt decide to add it because I thought it was Irreleavant

Comment: What is the error (In the console) ?

Comment: What is error line? Does it show error line?

Comment: Your code is incomplete since it does not contain the function createProfile


Here is another answer: As per your question, firebase returns nil value and following function has a block of firebase, which returns optional values for user &amp; error, that you are forcefully unwrapping.
Try this code, and let me know value for user.
func signUpAction(){
guard let email = Email.text, let password = Password.text else{
print("Something is Happenening")
return}
print(email)
print(password)
//adds user under the authentication tab on firebase
firebaseAuth.createUser(withEmail: email, password: password) { (user, err) in
if err != nil {
print(err!)
print(email)
print(password)
return  }
else{
// As per your question, firebase returns nil here
print("user - \(user)")
if let userInstance = user {
self.createProfile(userInstance)
}
self.performSegue(withIdentifier: "signUpToMainScene", sender: nil) }

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 1, 'CommentCount': 4, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2017:00:07/25/17', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<ios><firebase><swift3><firebase-authentication><option-type>', 'Title': 'Fatal Error unexpectedly found nil while unwrapping an optional value [Firebase]', 'ViewCount': 494, 'id': 45294967, 'user_AboutMe': None, 'user_AccountId': 7096158.0, 'user_DisplayName': 'Jordan Tyner', 'user_Reputation': 11.0, 'user_Views': 4.0, 'user_id': 5429666.0}}