I need to get the nested array values, I need the user data and I need token data. I need this data because I need to initialize session variables to be able to use them in my application.
For example the user name, fotografia, managed to get the data rc and msg that are in the main array but I could not get the values of nested arrays
["rc": 00,
"user": {
"__v" = 0;
deviceId = "";
email = "[email protected]";
fullName = "SMITH JOHN ";
lastName = "SMITH ";
modifiedAt = "2016-12-16T06:08:58.856Z";
name = "JOHN ";
photo = "";
provider = "";
"provider_id" = "";
status = 01;
tel = 3333333333;
typeUser = USER;
username = "[email protected]";
}, "token": {
"__v" = 0;
"_id" = 585384e3ccc4;
createdAt = "2016-12-16T08:10:03.407Z";
userId = 585384e3ccc4;
value = Z4WedlAzhdkap;
}, "msg": success]
@IBAction func btnLogin(_ sender: Any) {
let gsUtil=GSUtil()
let user = txtUser.text!
let password = txtPass.text!
let credentialData = "\(user):\(password)".data(using: String.Encoding.utf8)!
let base64Credentials = credentialData.base64EncodedString(options: [])
let headers = ["Authorization": "Basic \(base64Credentials)"]
Alamofire.request(gsUtil.getCompleteURI()+"user/auth/",
method: .post,
parameters: nil,
encoding: URLEncoding.default,
headers:headers)
.validate()
.responseJSON { response in
if response.result.value != nil{
let json = response.result.value as? [String: Any]
let rc=json?["rc"]
let msg=json?["msg"]
if rc as? String=="00"
{
self.showMessage(msg: msg as! String)
}
}else{
self.showMessage(msg: "Error en los datos")
}
}
}
I do not really get the problem. You already pretty much did what you need to do (only one step further):
I would start by getting rid of all the pesky optionals using a lot of if let
s:
if let json = input as? [String: Any] {
let msg = json["msg"]
if let rc = json["rc"] as? String, rc == "00" {
self.showMessage(msg: msg as! String)
}
if let user = json["user"] as? [String : Any] {
print(user)
}
if let token = json["token"] as? [String : Any] {
print(token)
}
} else {
self.showMessage(msg: "Error en los datos")
}