Error of "unwrapping an Optional value" in login screen transition

3

Whenever I will make the screen pass with login and password my application to 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 have already checked the returns of the functions, if it had some wrong Outlet, it's all right. This error occurred also in a simple screen transition, with no content, just drag and pull, I do not know what it can be. It prints the user and the id normally, but at the time of going to the other screen gives error. Here is the part of my code where the application for:

@IBAction func logar(sender: AnyObject) {

    self.myProgress.hidden = false
    self.myProgress.startAnimating()

    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
            self.myProgress.stopAnimating()

            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)
            print(self.usuarioAtual.msgError)
            self.myProgress.hidden = true
            self.myProgress.stopAnimating()
        }
    })

}

I think that what Xcode is saying is null is navigationController , but I do not know why, I use that same structure to pass the other screens and they work, even yesterday it was working normally, without changes this error appeared.

    
asked by anonymous 26.11.2015 / 04:18

1 answer

1

Your problem is divided into two parts.

1 - Optional Values.

This problem may not be in your add function, but in your class, where you are declaring your variables. Once you start a variable with no value, you must pass ? so that it becomes Optional. that is, by allowing it to be nill .

Example:

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

If you declare in 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 login function you make sure that the Email field is in value, you would put ! so that it "believe in your word that yes, this field has value", case contratorio, will give a fatal error and may crash your app.

So far so good, it's only a glance 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 get the getLogin function to be void the type signature should be declared like this: -> Void and not ->() .

    
26.11.2015 / 13:30