1

I am trying to present a view controller after I sign up but I keep getting the error

Thread 1: Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value

I am using Firebase, but I don't know if this anything to do with it.

    Auth.auth().createUser(withEmail: email, password: password, completion: {(user, error) in

        guard error == nil else{
            AlertController.showAlert(self, title: "Error", message: error!.localizedDescription)
            return
        }
        guard let user = user else { return }
        print(user.user.email ?? "Missing Email")
        print(user.user.uid)
        let changeRequest = user.user.createProfileChangeRequest()
        changeRequest.displayName = username
        changeRequest.commitChanges(completion: { (error) in

            guard error == nil else{

                AlertController.showAlert(self, title: "Error", message:  error!.localizedDescription)
                return
            }

            let homePage = HomeView()
            self.present(homePage, animated: true, completion: nil) 
        })
    })

This is where I get the error:

let homePage = HomeView()
self.present(homePage, animated: true, completion: nil)

I also get the same error for the view controller I am trying to present regarding the username This is the code from that view controller:

    override func viewDidLoad() {
        super.viewDidLoad()
        guard let username = Auth.auth().currentUser?.displayName else {return}
        welcome.text = "Hello \(username)"
        welcome.textColor = UIColor.systemGreen

This is where the error occurs:

welcome.text = "Hello (username)"

ketaki Damale
  • 562
  • 6
  • 23
  • 1
    when it crashes, can you print out what `welcome` is? your outlets might not be set. – kbunarjo May 18 '20 at 06:18
  • Did you connect *welcome* label to UILabel in storyboard. If you didnt connect api crashed because of nil *welcome* – Nabeel Nazir May 18 '20 at 06:19
  • I did connect the welcome label to UILabel in the storyboard but it is still not working. – user12985516 May 18 '20 at 06:32
  • I also made sure to get rid of any potential duplicates but even then it fails to work – user12985516 May 18 '20 at 06:33
  • clean build your code ... check welcome is connected – Jawad Ali May 18 '20 at 09:31
  • Can you clarify where exactly you're getting the error - what line? Is this macOS or iOS? Also, depending on where the .xib file is and what it's name is will determine how you instantiate the controller - this `let homePage = HomeView()` may not work without additional supporting code. If iOS see here [UIViewController](https://developer.apple.com/documentation/uikit/uiviewcontroller) for some options on adding the viewController object. – Jay May 20 '20 at 16:08

1 Answers1

0

Error is in the way you are initialising your Controller.

let homePage = HomeView()
self.present(homePage, animated: true, completion: nil)

This is not the correct way to initialise a controller.

if let homePage = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "HomeViewController") {
    self.present(homePage, animated: true, completion: nil)
}

Replace Main with name of your storyboard that contains the controller and HomeViewController with the storyboard identifier of your controller.

udbhateja
  • 914
  • 6
  • 16