线程1:在使用Alamofire进行登录时发出SIGABRT信号。
线程1:在使用Alamofire进行登录时发出SIGABRT信号。
我试图用Alamofire制作一个登录按钮,但我得到了标题中提到的错误,我的代码有什么问题吗?谢谢。
@IBAction func buttonLogin(_ sender: UIButton) {
//获取用户名和密码
let parameters: Parameters=[
"usuario":textFieldUserName.text!,
"password":textFieldPassword.text!
]
//发起POST请求
Alamofire.request("一个JSON页面的URL", method: .post, parameters: parameters).responseString { response in
//打印响应
print(response)
//从服务器获取JSON值
if let result = response.result.value {
let jsonData = result as NSObject
//从响应中获取用户
let user = jsonData.value(forKey: "user") as! NSArray
//获取用户值
let userId = user.value(forKey: "nombre") as! String
let userName = user.value(forKey: "usuario") as! String
let userPassword = user.value(forKey: "password") as! String
//将用户值保存到默认值
self.defaultValues.set(userId, forKey: "nombre")
self.defaultValues.set(userName, forKey: "usuario")
self.defaultValues.set(userPassword, forKey: "password")
//切换屏幕
let profileViewController = self.storyboard?.instantiateViewController(withIdentifier: "ProfileViewcontroller") as! ProfileViewController
self.navigationController?.pushViewController(profileViewController, animated: true)
self.dismiss(animated: false, completion: nil)
}else{
//无效凭证时的错误消息
self.labelMessage.text = "用户名或密码无效"
}
}
}
打印错误为:
*** 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
发生在:
//从响应中获取用户
let user = jsonData.value(forKey: "user") as! NSArray