swift – "unwrapping an Optional value" error on login screen transition


Whenever I go to the screen pass with login and password, my application stops with the error below:

fatal error: unexpectedly found nil while unwrapping an Optional value

function signature specialization of Swift.(_fatalErrorMessage(Swift.StaticString, Swift.StaticString, Swift.StaticString, Swift.UInt) -> ()).(closure #2)

I've already checked the function returns, if there was any wrong Outlet, everything is fine. This error also occurred in a simple screen transition, without any content, just drag and pull, I don't know what it could be. It prints the user and id normally, but when going to the other screen it gives an error. Below is the part of my code where the application stops:

@IBAction func logar(sender: AnyObject) {

    self.myProgress.hidden = false

    let parametros: NSDictionary = ["email":emailField.text!, "senha":senhaField.text!]

    controller.getLogin(parametros, handlerUser: {(usuario) -> () in
        self.usuarioAtual = usuario

        if self.usuarioAtual.msgError == "" {

            self.emailField.text = ""
            self.senhaField.text = ""

            self.myProgress.hidden = true

            let storyBoard = UIStoryboard(name: "Main", bundle: nil)
            let accessNavigation = storyBoard.instantiateViewControllerWithIdentifier("AccessViewController")
            LoginViewController.sharedInstance.usuarioAtual = self.usuarioAtual
            self.navigationController?.pushViewController(accessNavigation, animated: true)

        } else {

            Alerta(controller: self).erro("Desculpe!", message: self.usuarioAtual.msgError)
            self.myProgress.hidden = true


I believe that what Xcode is saying is null is the navigationController , but I don't know why, I use this same structure to pass the other screens and they work, just yesterday it was working normally, without changes this error appeared.


Your problem is divided into two parts.

1 – Optional Values.

It could be that this problem is not in your add function, but in your class, where you are declaring your variables. Once you initialize a variable with no value you need to pass the ? for it to become Optional. that is, allowing it to remain nill .


class MyClass{
    var a:String
    var b:String

If you declare it this way in a playground you will notice the various error messages saying that the class itself is null and its attributes too, because none of its attributes are initialized.

This will work:

class MyClass{
    var a:String?
    var b:String?

Let's say that in your log function you make sure that the Email field has a value, you would put ! so that he "takes his word that yes, this field has value", otherwise, it will give a fatal error and may crash your app.

So far so good, just take a look at your class if the variables are being initialized and if not how to prevent this problem from happening.

2 – Return of function

If you are intending to make the getLogin function void the type signature should be declared like this: -> Void and not ->() .

Scroll to Top