0

I've narrowed down the error to this block of code. I need to define something at optional but I'm not sure where.

    @IBAction func SignupWithFacebook(sender: AnyObject) {
    var permissionsArray = ["user_about_me", "user_birthday", "email"]
    PFFacebookUtils.logInWithPermissions(permissionsArray, block: { (user: PFUser?, error: NSError!) -> Void in
        if (user == nil) {
            let errormessage = error.userInfo!["error"] as NSString
            var facebookLoginError = UIAlertController(title: "Error While Logging", message: "\(errormessage)", preferredStyle: .Alert)
            var okButton = UIAlertAction(title: "OK", style: .Default, handler: nil)
            facebookLoginError.addAction(okButton)
            self.presentViewController(facebookLoginError, animated: true, completion: nil)

        }

    })

}

Any help would be greatly appreciated.

1 Answers1

0

Your code assumes that if user is nil then error will exist. I don't know if you can make that guarantee. Try this instead:

@IBAction func SignupWithFacebook(sender: AnyObject) {
    let permissionsArray = ["user_about_me", "user_birthday", "email"]
    PFFacebookUtils.logInWithPermissions(permissionsArray) { (user, error) -> Void in
        if user == nil {
            if let e = error {
                let errormessage = e.userInfo!["error"] as NSString
                let facebookLoginError = UIAlertController(title: "Error While Logging", message: "\(errormessage)", preferredStyle: .Alert)
                let okButton = UIAlertAction(title: "OK", style: .Default, handler: nil)
                facebookLoginError.addAction(okButton)
                self.presentViewController(facebookLoginError, animated: true, completion: nil)
            }
        }
    }
}
Daniel T.
  • 32,821
  • 6
  • 50
  • 72