“线程1:EXC_BAD_INSTRUCTION(代码= EXC_I386_INVOP,子代码= 0x0)”?

时间:2017-01-04 10:35:15

标签: ios swift

这是我的快捷代码 我将在此行中收到以上错误:guard (newUser["status"] as! Int != 0)

@IBAction func signInButton(_ sender: UIButton) {
    if validator(){
        DispatchQueue.global(priority: DispatchQueue.GlobalQueuePriority.default).async(execute: {

            let datas:[String:String] = ["email":self.emailField.text!,"name": self.nameField.text!,"password" : self.passwordField.text!]
            DispatchQueue.main.async {
                SwiftSpinner.show("Signin' in...")
            }
            let newUser:NSDictionary = self.marketcloud!.createUser(datas)
            print(newUser)
            DispatchQueue.main.async {
                SwiftSpinner.hide()
                guard (newUser["status"] as! Int != 0) else {
                    let alertController = UIAlertController(title: "Error", message: "Email already in use. Try with a different one!", preferredStyle: .alert)
                    alertController.addAction(UIAlertAction(title: "Close",
                        style: UIAlertActionStyle.destructive,
                        handler: nil))
                    self.present(alertController, animated: true, completion: nil)
                    return
                }
                let alertController = UIAlertController(title: "Ok!", message: "User created successfully!", preferredStyle: .alert)
                alertController.addAction(UIAlertAction(title: "Close", style: UIAlertActionStyle.default, handler: {(action:UIAlertAction) in
                    UserData.setLastRegisteredUser(self.emailField.text!, password: self.passwordField.text!);
                    print("Setted UserData \n \(UserData.getLastRegistedUserEmail(),UserData.getLastRegisteredUserPassword())")
                    //returns to the login view
                    let next = self.storyboard!.instantiateViewController(withIdentifier: "viewController") as! ViewController
                    next.downloadProducts = false
                    next.load = true                        
                    self.navigationController?.pushViewController(next, animated: true)
                }));
                self.present(alertController, animated: true, completion: nil)
            }
        })
    }
}

1 个答案:

答案 0 :(得分:1)

尝试了解guard的工作原理。

条件必须是let或布尔表达式

的可选绑定
guard let status = newUser["status"] as? Int, status != 0 else { ...

Swift没有括号。