If Let Error - Initializer for conditional binding should have an optional type, and not '[NSObject: AnyObject]'

Here is my code.

    PFUser.logInWithUsernameInBackground(email, password: password) { (user: PFUser!, error: NSError!) -> Void in
        if user != nil {
            PushNotication.parsePushUserAssign()
            ProgressHUD.showSuccess("Welcome back, \(user[PF_USER_FULLNAME])!")
            self.dismissViewControllerAnimated(true, completion: nil)
        } else {
            if let info = error.userInfo {
                ProgressHUD.showError(info["error"] as! String)
            }
        }
    }

This causes an error, for example: "The initializer for the conditional binding must be of an optional type, and not" [NSObject: AnyObject] "" Is there anyone who knows the solution?

+4
source share
2 answers

error.userInfonot optional, this is the type the [NSObject : AnyObject]compiler hinted at. There is no need to deploy it with the help if let, it will never be nil.

You can replace

if let info = error.userInfo {
    ProgressHUD.showError(info["error"] as! String)
}

from

ProgressHUD.showError(error.userInfo["error"] as! String)

if you are sure that the value will be String.

Otherwise, the dictionary value must be safely expanded and omitted as a string. Example:

if let errorString = error.userInfo["error"] as? String {
    ProgressHUD.showError(errorString)
}
+4
source

, :

ProgressHUD.showError(error.userInfo["error"] as! String)

.

-2

Source: https://habr.com/ru/post/1620407/


All Articles