"unwrapping an Optional value" error in login screen transition

Asked

Viewed 73 times

3

Whenever I will do 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 checked the function returns, if there was any wrong outlet, it’s all right. This error also occurred in a simple screen transition, without any content, just drags and pulls, I do not know what can be. It prints the user and the id normally, but when passing to the other screen gives error. Follows below the part of my code in which the application to:

@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 believe that what the Xcode is saying is null is the navigationController, but do not know why, I use this same structure to pass the other screens and work, even yesterday was working normally, no changes appeared this error.

1 answer

1


Your problem is divided into two parts.

1 - Optional Values.

It may be that this problem is not in your job add It’s in your class, where you’re declaring your variables. Once you start a variable with no value you need to pass the ? to make it optional. that is, allowing her to stay nill.

Example:

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

If you declare this way in a playground you will notice the various error messages saying that the class itself is null and its attributes also, 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 worth, you would put ! so that he "believe your word that yes, this field has value", otherwise it will give fatal error and may lock 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 - Function return

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.