线程1:尝试使用Alamofire登录时发出SIGABRT信号

时间:2018-02-02 23:58:20

标签: swift alamofire nsunknownkeyexception

我正在尝试使用Alamofire创建一个登录按钮,但是我收到了我在标题上说的错误,我的代码有问题吗?谢谢。

@IBAction func buttonLogin(_ sender: UIButton) {
    //getting the username and password
    let parameters: Parameters=[
        "usuario":textFieldUserName.text!,
        "password":textFieldPassword.text!
    ]

    //making a post request
    Alamofire.request("A JSON PAGE", method: .post, parameters: parameters).responseString { response in
        //printing response
        print(response)

        //getting the json value from the server
        if let result = response.result.value {
            let jsonData = result as NSObject

            //getting the user from response
            let user = jsonData.value(forKey: "user") as! NSArray

            //getting user values
            let userId = user.value(forKey: "nombre") as! String
            let userName = user.value(forKey: "usuario") as! String
            let userPassword = user.value(forKey: "password") as! String

            //saving user values to defaults
            self.defaultValues.set(userId, forKey: "nombre")
            self.defaultValues.set(userName, forKey: "usuario")
            self.defaultValues.set(userPassword, forKey: "password")

            //switching the screen
            let profileViewController = self.storyboard?.instantiateViewController(withIdentifier: "ProfileViewcontroller") as! ProfileViewController
            self.navigationController?.pushViewController(profileViewController, animated: true)

            self.dismiss(animated: false, completion: nil)
        }else{
            //error message in case of invalid credential
            self.labelMessage.text = "Invalid username or password"
        }
    }
}

打印错误是:

*** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[ valueForUndefinedKey:]: this class is not key value coding-compliant for the key user.'
*** First throw call stack:
(0x18542a364 0x184670528 0x18542a02c 0x185dde434 0x185d24e20 0x1003e61a4 0x100abb708 0x100ab797c 0x100a76d38 0x10188d2cc 0x10188d28c 0x101891ea0 0x1853d2544 0x1853d0120 0x1852efe58 0x18719cf84 0x18e96f67c 0x1003e9500 0x184e0c56c)
libc++abi.dylib: terminating with uncaught exception of type NSException

at:

//getting the user from response
let user = jsonData.value(forKey: "user") as! NSArray

0 个答案:

没有答案