0

I am trying to make a login button with Alamofire, but I'm getting the error I said on the title, is there something wrong with my code? Thanks.

@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"
        }
    }
}

The print error is:

*** 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
rmaddy
  • 314,917
  • 42
  • 532
  • 579
A. Sierra
  • 1
  • 1
  • 1
    Possible duplicate of [Swift error : signal SIGABRT how to solve it](https://stackoverflow.com/questions/43546497/swift-error-signal-sigabrt-how-to-solve-it) – John Montgomery Feb 03 '18 at 00:15
  • Is your button connected to the action method? – Xcoder Feb 03 '18 at 00:16
  • @JohnMontgomery no it isn't. – A. Sierra Feb 03 '18 at 00:17
  • @vadian Yes it is. – A. Sierra Feb 03 '18 at 00:17
  • Can you try posting the debug area when you get that error? That's the part where all of the print() happens. – Xcoder Feb 03 '18 at 00:19
  • @JohnMontgomery It's not really a duplicate if OP's outlets are connected and the code from both questions is different. – Xcoder Feb 03 '18 at 00:20
  • @vadian I edited it again, look. – A. Sierra Feb 03 '18 at 00:29
  • You are using `responseString`, so your `jsonData` is just a `String`, internally it's just an `NSString` (`__NSCFString` in your case), which cannot respond to KVC access `user` and causes SIGABRT. You may need to use `responseJSON`. One more, `value(forKey:)` is not a recommended way to get values from JSON response. Better cast it to `Dictionary` and use subscript. – OOPer Feb 03 '18 at 00:49
  • Clearly from the error, the key "user" does not have any value associated with it. Check your key-value pairs again. – Xcoder Feb 03 '18 at 01:01
  • check **class is not key value coding-compliant for the key user** Do NSArray is having **user**, Second Try checking JSON format too, Returned format is Array or Dictionary ? – iOS Geek Feb 03 '18 at 04:40
  • Yeah you've messed up parsing your JSON I think – Allison Feb 03 '18 at 07:04

0 Answers0