Could not cast value of type '__NSArray0' to 'NSDictionary'
when I parse My json response
and this my error
Could not cast value of type '__NSArray0' (0x104bcd838) to 'NSDictionary' (0x104bcf818).
this is my code
@IBAction func LoginAction(_ sender: Any) {
let parameters: Parameters=[
"email":emailUser.text!,
"password":passWord.text!,
]
let url2 = "http://marwen1994.alwaysdata.net/Carpooling/public/loginpost.php"
Alamofire.request(url2, method: .post, parameters: parameters).responseJSON
{
response in
let A = response.result.value as! Dictionary<String,Any>
let list = A["items"] as! Dictionary<String,Any>
let nom = list["name"] as! String
let pass = A["password"] as! String
let email = list["email"] as! String
let adresse = A["adresse"] as! String
let DateNaissance = A["DateNaissance"] as! String
let id = A["id"] as! Int
let numTel = A["num_tel"] as! Int
if(pass == self.passWord.text)
{
UserDefaults.standard.set(true, forKey: "ConnectionStatus")
UserDefaults.standard.set(id, forKey: "ConnectedID")
UserDefaults.standard.set(nom, forKey: "nom")
// UserDefaults.standard.set(prenom, forKey: "prenom")
UserDefaults.standard.set(email, forKey: "email")
UserDefaults.standard.set(adresse, forKey: "adresse")
UserDefaults.standard.set(pass, forKey: "motDePasse")
UserDefaults.standard.set(numTel, forKey: "numTel")
UserDefaults.standard.set(DateNaissance, forKey: "DateNaissance")
self.performSegue(withIdentifier: "toProfile", sender: nil)
}
else{
let alert = UIAlertController(title: "Woah!!!", message: "You inserted a wrong email or a wrong password! Please enter a valid mail and password.", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "Ok", style: .cancel, handler: nil))
self.present(alert, animated: true)
}
}
}
ios
add a comment |
when I parse My json response
and this my error
Could not cast value of type '__NSArray0' (0x104bcd838) to 'NSDictionary' (0x104bcf818).
this is my code
@IBAction func LoginAction(_ sender: Any) {
let parameters: Parameters=[
"email":emailUser.text!,
"password":passWord.text!,
]
let url2 = "http://marwen1994.alwaysdata.net/Carpooling/public/loginpost.php"
Alamofire.request(url2, method: .post, parameters: parameters).responseJSON
{
response in
let A = response.result.value as! Dictionary<String,Any>
let list = A["items"] as! Dictionary<String,Any>
let nom = list["name"] as! String
let pass = A["password"] as! String
let email = list["email"] as! String
let adresse = A["adresse"] as! String
let DateNaissance = A["DateNaissance"] as! String
let id = A["id"] as! Int
let numTel = A["num_tel"] as! Int
if(pass == self.passWord.text)
{
UserDefaults.standard.set(true, forKey: "ConnectionStatus")
UserDefaults.standard.set(id, forKey: "ConnectedID")
UserDefaults.standard.set(nom, forKey: "nom")
// UserDefaults.standard.set(prenom, forKey: "prenom")
UserDefaults.standard.set(email, forKey: "email")
UserDefaults.standard.set(adresse, forKey: "adresse")
UserDefaults.standard.set(pass, forKey: "motDePasse")
UserDefaults.standard.set(numTel, forKey: "numTel")
UserDefaults.standard.set(DateNaissance, forKey: "DateNaissance")
self.performSegue(withIdentifier: "toProfile", sender: nil)
}
else{
let alert = UIAlertController(title: "Woah!!!", message: "You inserted a wrong email or a wrong password! Please enter a valid mail and password.", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "Ok", style: .cancel, handler: nil))
self.present(alert, animated: true)
}
}
}
ios
2
Well the error is pretty obvious isn't it?let A = response.result.value as! Dictionary<String,Any>
. What does the json response look like?
– Joakim Danielson
Jan 3 at 15:51
{ "items": [ { "id": "14", "unique_id": "5bb7682e2031f7.15799495", "name": "test", "email": "test@test.com", "encrypted_password": "5hQXusqX1GyyMO5ZvOrvJbkHvFMxOTQwYmYwM2Ew", "salt": "1940bf03a0", "created_at": "2018-10-05 15:33:34", "updated_at": null, "num_tel": "+21695502248", "image": "14", "adresse": "tunis-ariana", "DateNaissance": "12/14/2018" } ] }
– Brahim CHEBBI
Jan 3 at 15:53
1
So,A["items"]
is an array that contains a dictionary rather than a dictionary itself. In general, don't use!
unless you really know what the correct type will be.
– Phillip Mills
Jan 3 at 15:59
(Learn to) read the JSON. It's pretty easy. There are only 2 (two) collection types:is array (Swift
[[String:Any]]
or[Dictionary<String,Any>]
),{}
is dictionary (Swift[String:Any]
orDictionary<String,Any>
).
– vadian
Jan 3 at 16:22
add a comment |
when I parse My json response
and this my error
Could not cast value of type '__NSArray0' (0x104bcd838) to 'NSDictionary' (0x104bcf818).
this is my code
@IBAction func LoginAction(_ sender: Any) {
let parameters: Parameters=[
"email":emailUser.text!,
"password":passWord.text!,
]
let url2 = "http://marwen1994.alwaysdata.net/Carpooling/public/loginpost.php"
Alamofire.request(url2, method: .post, parameters: parameters).responseJSON
{
response in
let A = response.result.value as! Dictionary<String,Any>
let list = A["items"] as! Dictionary<String,Any>
let nom = list["name"] as! String
let pass = A["password"] as! String
let email = list["email"] as! String
let adresse = A["adresse"] as! String
let DateNaissance = A["DateNaissance"] as! String
let id = A["id"] as! Int
let numTel = A["num_tel"] as! Int
if(pass == self.passWord.text)
{
UserDefaults.standard.set(true, forKey: "ConnectionStatus")
UserDefaults.standard.set(id, forKey: "ConnectedID")
UserDefaults.standard.set(nom, forKey: "nom")
// UserDefaults.standard.set(prenom, forKey: "prenom")
UserDefaults.standard.set(email, forKey: "email")
UserDefaults.standard.set(adresse, forKey: "adresse")
UserDefaults.standard.set(pass, forKey: "motDePasse")
UserDefaults.standard.set(numTel, forKey: "numTel")
UserDefaults.standard.set(DateNaissance, forKey: "DateNaissance")
self.performSegue(withIdentifier: "toProfile", sender: nil)
}
else{
let alert = UIAlertController(title: "Woah!!!", message: "You inserted a wrong email or a wrong password! Please enter a valid mail and password.", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "Ok", style: .cancel, handler: nil))
self.present(alert, animated: true)
}
}
}
ios
when I parse My json response
and this my error
Could not cast value of type '__NSArray0' (0x104bcd838) to 'NSDictionary' (0x104bcf818).
this is my code
@IBAction func LoginAction(_ sender: Any) {
let parameters: Parameters=[
"email":emailUser.text!,
"password":passWord.text!,
]
let url2 = "http://marwen1994.alwaysdata.net/Carpooling/public/loginpost.php"
Alamofire.request(url2, method: .post, parameters: parameters).responseJSON
{
response in
let A = response.result.value as! Dictionary<String,Any>
let list = A["items"] as! Dictionary<String,Any>
let nom = list["name"] as! String
let pass = A["password"] as! String
let email = list["email"] as! String
let adresse = A["adresse"] as! String
let DateNaissance = A["DateNaissance"] as! String
let id = A["id"] as! Int
let numTel = A["num_tel"] as! Int
if(pass == self.passWord.text)
{
UserDefaults.standard.set(true, forKey: "ConnectionStatus")
UserDefaults.standard.set(id, forKey: "ConnectedID")
UserDefaults.standard.set(nom, forKey: "nom")
// UserDefaults.standard.set(prenom, forKey: "prenom")
UserDefaults.standard.set(email, forKey: "email")
UserDefaults.standard.set(adresse, forKey: "adresse")
UserDefaults.standard.set(pass, forKey: "motDePasse")
UserDefaults.standard.set(numTel, forKey: "numTel")
UserDefaults.standard.set(DateNaissance, forKey: "DateNaissance")
self.performSegue(withIdentifier: "toProfile", sender: nil)
}
else{
let alert = UIAlertController(title: "Woah!!!", message: "You inserted a wrong email or a wrong password! Please enter a valid mail and password.", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "Ok", style: .cancel, handler: nil))
self.present(alert, animated: true)
}
}
}
ios
ios
edited Jan 3 at 15:51
Joakim Danielson
10.4k3725
10.4k3725
asked Jan 3 at 15:47
Brahim CHEBBIBrahim CHEBBI
1
1
2
Well the error is pretty obvious isn't it?let A = response.result.value as! Dictionary<String,Any>
. What does the json response look like?
– Joakim Danielson
Jan 3 at 15:51
{ "items": [ { "id": "14", "unique_id": "5bb7682e2031f7.15799495", "name": "test", "email": "test@test.com", "encrypted_password": "5hQXusqX1GyyMO5ZvOrvJbkHvFMxOTQwYmYwM2Ew", "salt": "1940bf03a0", "created_at": "2018-10-05 15:33:34", "updated_at": null, "num_tel": "+21695502248", "image": "14", "adresse": "tunis-ariana", "DateNaissance": "12/14/2018" } ] }
– Brahim CHEBBI
Jan 3 at 15:53
1
So,A["items"]
is an array that contains a dictionary rather than a dictionary itself. In general, don't use!
unless you really know what the correct type will be.
– Phillip Mills
Jan 3 at 15:59
(Learn to) read the JSON. It's pretty easy. There are only 2 (two) collection types:is array (Swift
[[String:Any]]
or[Dictionary<String,Any>]
),{}
is dictionary (Swift[String:Any]
orDictionary<String,Any>
).
– vadian
Jan 3 at 16:22
add a comment |
2
Well the error is pretty obvious isn't it?let A = response.result.value as! Dictionary<String,Any>
. What does the json response look like?
– Joakim Danielson
Jan 3 at 15:51
{ "items": [ { "id": "14", "unique_id": "5bb7682e2031f7.15799495", "name": "test", "email": "test@test.com", "encrypted_password": "5hQXusqX1GyyMO5ZvOrvJbkHvFMxOTQwYmYwM2Ew", "salt": "1940bf03a0", "created_at": "2018-10-05 15:33:34", "updated_at": null, "num_tel": "+21695502248", "image": "14", "adresse": "tunis-ariana", "DateNaissance": "12/14/2018" } ] }
– Brahim CHEBBI
Jan 3 at 15:53
1
So,A["items"]
is an array that contains a dictionary rather than a dictionary itself. In general, don't use!
unless you really know what the correct type will be.
– Phillip Mills
Jan 3 at 15:59
(Learn to) read the JSON. It's pretty easy. There are only 2 (two) collection types:is array (Swift
[[String:Any]]
or[Dictionary<String,Any>]
),{}
is dictionary (Swift[String:Any]
orDictionary<String,Any>
).
– vadian
Jan 3 at 16:22
2
2
Well the error is pretty obvious isn't it?
let A = response.result.value as! Dictionary<String,Any>
. What does the json response look like?– Joakim Danielson
Jan 3 at 15:51
Well the error is pretty obvious isn't it?
let A = response.result.value as! Dictionary<String,Any>
. What does the json response look like?– Joakim Danielson
Jan 3 at 15:51
{ "items": [ { "id": "14", "unique_id": "5bb7682e2031f7.15799495", "name": "test", "email": "test@test.com", "encrypted_password": "5hQXusqX1GyyMO5ZvOrvJbkHvFMxOTQwYmYwM2Ew", "salt": "1940bf03a0", "created_at": "2018-10-05 15:33:34", "updated_at": null, "num_tel": "+21695502248", "image": "14", "adresse": "tunis-ariana", "DateNaissance": "12/14/2018" } ] }
– Brahim CHEBBI
Jan 3 at 15:53
{ "items": [ { "id": "14", "unique_id": "5bb7682e2031f7.15799495", "name": "test", "email": "test@test.com", "encrypted_password": "5hQXusqX1GyyMO5ZvOrvJbkHvFMxOTQwYmYwM2Ew", "salt": "1940bf03a0", "created_at": "2018-10-05 15:33:34", "updated_at": null, "num_tel": "+21695502248", "image": "14", "adresse": "tunis-ariana", "DateNaissance": "12/14/2018" } ] }
– Brahim CHEBBI
Jan 3 at 15:53
1
1
So,
A["items"]
is an array that contains a dictionary rather than a dictionary itself. In general, don't use !
unless you really know what the correct type will be.– Phillip Mills
Jan 3 at 15:59
So,
A["items"]
is an array that contains a dictionary rather than a dictionary itself. In general, don't use !
unless you really know what the correct type will be.– Phillip Mills
Jan 3 at 15:59
(Learn to) read the JSON. It's pretty easy. There are only 2 (two) collection types:
is array (Swift [[String:Any]]
or [Dictionary<String,Any>]
), {}
is dictionary (Swift [String:Any]
or Dictionary<String,Any>
).– vadian
Jan 3 at 16:22
(Learn to) read the JSON. It's pretty easy. There are only 2 (two) collection types:
is array (Swift [[String:Any]]
or [Dictionary<String,Any>]
), {}
is dictionary (Swift [String:Any]
or Dictionary<String,Any>
).– vadian
Jan 3 at 16:22
add a comment |
2 Answers
2
active
oldest
votes
Hard to know where the error is without seeing the actual JSON response, but probably this is an Array and not a Dictionary
let list = A["items"] as! Dictionary<String,Any>
Should be something like
let list = A["items"] as! Array
{ "items": [ { "id": "14", "unique_id": "5bb7682e2031f7.15799495", "name": "test", "email": "test@test.com", "encrypted_password": "5hQXusqX1GyyMO5ZvOrvJbkHvFMxOTQwYmYwM2Ew", "salt": "1940bf03a0", "created_at": "2018-10-05 15:33:34", "updated_at": null, "num_tel": "+21695502248", "image": "14", "adresse": "tunis-ariana", "DateNaissance": "12/14/2018" } ] }
– Brahim CHEBBI
Jan 3 at 16:52
this the JSOn response
– Brahim CHEBBI
Jan 3 at 16:52
so yeah, what I said, items is an array, not a dictionary, as it only has one element you can dolet list = A["items"][0] as! as! Dictionary<String,Any>
, but if you manage the server and it will only return an item I would change it to return the object (dictionary) instead of the array. Also is not good that you return the password to the app
– jcesarmobile
Jan 3 at 17:02
add a comment |
If you could consider using Codable
this might help
struct JsonResponse: Decodable {
let items: [Item]
}
struct Item: Decodable {
let id: String
let name: String
let email: String
let encrypted_password: String
let num_tel: String
let adresse: String
let DateNaissance: String
}
The actual decoding is done like this
let decoder = JSONDecoder()
do {
let jsonResponse = try decoder.decode(JsonResponse.self, from: data)
for item in jsonResponse.items {
UserDefaults.standard.set(item.name, forKey: "nom")
UserDefaults.standard.set(item.email, forKey: "email")
//and so on
}
} catch {
print("Decode error: (error)")
}
data
is the json message in response
, I don't know how Alamofire works so you need to figure out yourself how to access it correctly.
There is an array inside "items" with a single element, access it like
if let A = response.result.value as? Dictionary<String,Any>, let list = A["items"][0] as? Dictionary<String,Any> {
}
this is my jsonresponse
– Brahim CHEBBI
Jan 4 at 15:00
{ "items": [ { "id": "14", "unique_id": "5bb7682e2031f7.15799495", "name": "test", "email": "test@test.com", "encrypted_password": "5hQXusqX1GyyMO5ZvOrvJbkHvFMxOTQwYmYwM2Ew", "salt": "1940bf03a0", "created_at": "2018-10-05 15:33:34", "updated_at": null, "num_tel": "+21695502248", "image": "14", "adresse": "tunis-ariana", "DateNaissance": "12/14/2018" } ] }
– Brahim CHEBBI
Jan 4 at 15:00
@BrahimCHEBBI any update here?
– Joakim Danielson
Jan 8 at 8:11
add a comment |
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54025572%2fcould-not-cast-value-of-type-nsarray0-to-nsdictionary%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Hard to know where the error is without seeing the actual JSON response, but probably this is an Array and not a Dictionary
let list = A["items"] as! Dictionary<String,Any>
Should be something like
let list = A["items"] as! Array
{ "items": [ { "id": "14", "unique_id": "5bb7682e2031f7.15799495", "name": "test", "email": "test@test.com", "encrypted_password": "5hQXusqX1GyyMO5ZvOrvJbkHvFMxOTQwYmYwM2Ew", "salt": "1940bf03a0", "created_at": "2018-10-05 15:33:34", "updated_at": null, "num_tel": "+21695502248", "image": "14", "adresse": "tunis-ariana", "DateNaissance": "12/14/2018" } ] }
– Brahim CHEBBI
Jan 3 at 16:52
this the JSOn response
– Brahim CHEBBI
Jan 3 at 16:52
so yeah, what I said, items is an array, not a dictionary, as it only has one element you can dolet list = A["items"][0] as! as! Dictionary<String,Any>
, but if you manage the server and it will only return an item I would change it to return the object (dictionary) instead of the array. Also is not good that you return the password to the app
– jcesarmobile
Jan 3 at 17:02
add a comment |
Hard to know where the error is without seeing the actual JSON response, but probably this is an Array and not a Dictionary
let list = A["items"] as! Dictionary<String,Any>
Should be something like
let list = A["items"] as! Array
{ "items": [ { "id": "14", "unique_id": "5bb7682e2031f7.15799495", "name": "test", "email": "test@test.com", "encrypted_password": "5hQXusqX1GyyMO5ZvOrvJbkHvFMxOTQwYmYwM2Ew", "salt": "1940bf03a0", "created_at": "2018-10-05 15:33:34", "updated_at": null, "num_tel": "+21695502248", "image": "14", "adresse": "tunis-ariana", "DateNaissance": "12/14/2018" } ] }
– Brahim CHEBBI
Jan 3 at 16:52
this the JSOn response
– Brahim CHEBBI
Jan 3 at 16:52
so yeah, what I said, items is an array, not a dictionary, as it only has one element you can dolet list = A["items"][0] as! as! Dictionary<String,Any>
, but if you manage the server and it will only return an item I would change it to return the object (dictionary) instead of the array. Also is not good that you return the password to the app
– jcesarmobile
Jan 3 at 17:02
add a comment |
Hard to know where the error is without seeing the actual JSON response, but probably this is an Array and not a Dictionary
let list = A["items"] as! Dictionary<String,Any>
Should be something like
let list = A["items"] as! Array
Hard to know where the error is without seeing the actual JSON response, but probably this is an Array and not a Dictionary
let list = A["items"] as! Dictionary<String,Any>
Should be something like
let list = A["items"] as! Array
answered Jan 3 at 15:57
jcesarmobilejcesarmobile
37k781126
37k781126
{ "items": [ { "id": "14", "unique_id": "5bb7682e2031f7.15799495", "name": "test", "email": "test@test.com", "encrypted_password": "5hQXusqX1GyyMO5ZvOrvJbkHvFMxOTQwYmYwM2Ew", "salt": "1940bf03a0", "created_at": "2018-10-05 15:33:34", "updated_at": null, "num_tel": "+21695502248", "image": "14", "adresse": "tunis-ariana", "DateNaissance": "12/14/2018" } ] }
– Brahim CHEBBI
Jan 3 at 16:52
this the JSOn response
– Brahim CHEBBI
Jan 3 at 16:52
so yeah, what I said, items is an array, not a dictionary, as it only has one element you can dolet list = A["items"][0] as! as! Dictionary<String,Any>
, but if you manage the server and it will only return an item I would change it to return the object (dictionary) instead of the array. Also is not good that you return the password to the app
– jcesarmobile
Jan 3 at 17:02
add a comment |
{ "items": [ { "id": "14", "unique_id": "5bb7682e2031f7.15799495", "name": "test", "email": "test@test.com", "encrypted_password": "5hQXusqX1GyyMO5ZvOrvJbkHvFMxOTQwYmYwM2Ew", "salt": "1940bf03a0", "created_at": "2018-10-05 15:33:34", "updated_at": null, "num_tel": "+21695502248", "image": "14", "adresse": "tunis-ariana", "DateNaissance": "12/14/2018" } ] }
– Brahim CHEBBI
Jan 3 at 16:52
this the JSOn response
– Brahim CHEBBI
Jan 3 at 16:52
so yeah, what I said, items is an array, not a dictionary, as it only has one element you can dolet list = A["items"][0] as! as! Dictionary<String,Any>
, but if you manage the server and it will only return an item I would change it to return the object (dictionary) instead of the array. Also is not good that you return the password to the app
– jcesarmobile
Jan 3 at 17:02
{ "items": [ { "id": "14", "unique_id": "5bb7682e2031f7.15799495", "name": "test", "email": "test@test.com", "encrypted_password": "5hQXusqX1GyyMO5ZvOrvJbkHvFMxOTQwYmYwM2Ew", "salt": "1940bf03a0", "created_at": "2018-10-05 15:33:34", "updated_at": null, "num_tel": "+21695502248", "image": "14", "adresse": "tunis-ariana", "DateNaissance": "12/14/2018" } ] }
– Brahim CHEBBI
Jan 3 at 16:52
{ "items": [ { "id": "14", "unique_id": "5bb7682e2031f7.15799495", "name": "test", "email": "test@test.com", "encrypted_password": "5hQXusqX1GyyMO5ZvOrvJbkHvFMxOTQwYmYwM2Ew", "salt": "1940bf03a0", "created_at": "2018-10-05 15:33:34", "updated_at": null, "num_tel": "+21695502248", "image": "14", "adresse": "tunis-ariana", "DateNaissance": "12/14/2018" } ] }
– Brahim CHEBBI
Jan 3 at 16:52
this the JSOn response
– Brahim CHEBBI
Jan 3 at 16:52
this the JSOn response
– Brahim CHEBBI
Jan 3 at 16:52
so yeah, what I said, items is an array, not a dictionary, as it only has one element you can do
let list = A["items"][0] as! as! Dictionary<String,Any>
, but if you manage the server and it will only return an item I would change it to return the object (dictionary) instead of the array. Also is not good that you return the password to the app– jcesarmobile
Jan 3 at 17:02
so yeah, what I said, items is an array, not a dictionary, as it only has one element you can do
let list = A["items"][0] as! as! Dictionary<String,Any>
, but if you manage the server and it will only return an item I would change it to return the object (dictionary) instead of the array. Also is not good that you return the password to the app– jcesarmobile
Jan 3 at 17:02
add a comment |
If you could consider using Codable
this might help
struct JsonResponse: Decodable {
let items: [Item]
}
struct Item: Decodable {
let id: String
let name: String
let email: String
let encrypted_password: String
let num_tel: String
let adresse: String
let DateNaissance: String
}
The actual decoding is done like this
let decoder = JSONDecoder()
do {
let jsonResponse = try decoder.decode(JsonResponse.self, from: data)
for item in jsonResponse.items {
UserDefaults.standard.set(item.name, forKey: "nom")
UserDefaults.standard.set(item.email, forKey: "email")
//and so on
}
} catch {
print("Decode error: (error)")
}
data
is the json message in response
, I don't know how Alamofire works so you need to figure out yourself how to access it correctly.
There is an array inside "items" with a single element, access it like
if let A = response.result.value as? Dictionary<String,Any>, let list = A["items"][0] as? Dictionary<String,Any> {
}
this is my jsonresponse
– Brahim CHEBBI
Jan 4 at 15:00
{ "items": [ { "id": "14", "unique_id": "5bb7682e2031f7.15799495", "name": "test", "email": "test@test.com", "encrypted_password": "5hQXusqX1GyyMO5ZvOrvJbkHvFMxOTQwYmYwM2Ew", "salt": "1940bf03a0", "created_at": "2018-10-05 15:33:34", "updated_at": null, "num_tel": "+21695502248", "image": "14", "adresse": "tunis-ariana", "DateNaissance": "12/14/2018" } ] }
– Brahim CHEBBI
Jan 4 at 15:00
@BrahimCHEBBI any update here?
– Joakim Danielson
Jan 8 at 8:11
add a comment |
If you could consider using Codable
this might help
struct JsonResponse: Decodable {
let items: [Item]
}
struct Item: Decodable {
let id: String
let name: String
let email: String
let encrypted_password: String
let num_tel: String
let adresse: String
let DateNaissance: String
}
The actual decoding is done like this
let decoder = JSONDecoder()
do {
let jsonResponse = try decoder.decode(JsonResponse.self, from: data)
for item in jsonResponse.items {
UserDefaults.standard.set(item.name, forKey: "nom")
UserDefaults.standard.set(item.email, forKey: "email")
//and so on
}
} catch {
print("Decode error: (error)")
}
data
is the json message in response
, I don't know how Alamofire works so you need to figure out yourself how to access it correctly.
There is an array inside "items" with a single element, access it like
if let A = response.result.value as? Dictionary<String,Any>, let list = A["items"][0] as? Dictionary<String,Any> {
}
this is my jsonresponse
– Brahim CHEBBI
Jan 4 at 15:00
{ "items": [ { "id": "14", "unique_id": "5bb7682e2031f7.15799495", "name": "test", "email": "test@test.com", "encrypted_password": "5hQXusqX1GyyMO5ZvOrvJbkHvFMxOTQwYmYwM2Ew", "salt": "1940bf03a0", "created_at": "2018-10-05 15:33:34", "updated_at": null, "num_tel": "+21695502248", "image": "14", "adresse": "tunis-ariana", "DateNaissance": "12/14/2018" } ] }
– Brahim CHEBBI
Jan 4 at 15:00
@BrahimCHEBBI any update here?
– Joakim Danielson
Jan 8 at 8:11
add a comment |
If you could consider using Codable
this might help
struct JsonResponse: Decodable {
let items: [Item]
}
struct Item: Decodable {
let id: String
let name: String
let email: String
let encrypted_password: String
let num_tel: String
let adresse: String
let DateNaissance: String
}
The actual decoding is done like this
let decoder = JSONDecoder()
do {
let jsonResponse = try decoder.decode(JsonResponse.self, from: data)
for item in jsonResponse.items {
UserDefaults.standard.set(item.name, forKey: "nom")
UserDefaults.standard.set(item.email, forKey: "email")
//and so on
}
} catch {
print("Decode error: (error)")
}
data
is the json message in response
, I don't know how Alamofire works so you need to figure out yourself how to access it correctly.
There is an array inside "items" with a single element, access it like
if let A = response.result.value as? Dictionary<String,Any>, let list = A["items"][0] as? Dictionary<String,Any> {
}
If you could consider using Codable
this might help
struct JsonResponse: Decodable {
let items: [Item]
}
struct Item: Decodable {
let id: String
let name: String
let email: String
let encrypted_password: String
let num_tel: String
let adresse: String
let DateNaissance: String
}
The actual decoding is done like this
let decoder = JSONDecoder()
do {
let jsonResponse = try decoder.decode(JsonResponse.self, from: data)
for item in jsonResponse.items {
UserDefaults.standard.set(item.name, forKey: "nom")
UserDefaults.standard.set(item.email, forKey: "email")
//and so on
}
} catch {
print("Decode error: (error)")
}
data
is the json message in response
, I don't know how Alamofire works so you need to figure out yourself how to access it correctly.
There is an array inside "items" with a single element, access it like
if let A = response.result.value as? Dictionary<String,Any>, let list = A["items"][0] as? Dictionary<String,Any> {
}
edited Jan 4 at 20:54
answered Jan 3 at 16:01
Joakim DanielsonJoakim Danielson
10.4k3725
10.4k3725
this is my jsonresponse
– Brahim CHEBBI
Jan 4 at 15:00
{ "items": [ { "id": "14", "unique_id": "5bb7682e2031f7.15799495", "name": "test", "email": "test@test.com", "encrypted_password": "5hQXusqX1GyyMO5ZvOrvJbkHvFMxOTQwYmYwM2Ew", "salt": "1940bf03a0", "created_at": "2018-10-05 15:33:34", "updated_at": null, "num_tel": "+21695502248", "image": "14", "adresse": "tunis-ariana", "DateNaissance": "12/14/2018" } ] }
– Brahim CHEBBI
Jan 4 at 15:00
@BrahimCHEBBI any update here?
– Joakim Danielson
Jan 8 at 8:11
add a comment |
this is my jsonresponse
– Brahim CHEBBI
Jan 4 at 15:00
{ "items": [ { "id": "14", "unique_id": "5bb7682e2031f7.15799495", "name": "test", "email": "test@test.com", "encrypted_password": "5hQXusqX1GyyMO5ZvOrvJbkHvFMxOTQwYmYwM2Ew", "salt": "1940bf03a0", "created_at": "2018-10-05 15:33:34", "updated_at": null, "num_tel": "+21695502248", "image": "14", "adresse": "tunis-ariana", "DateNaissance": "12/14/2018" } ] }
– Brahim CHEBBI
Jan 4 at 15:00
@BrahimCHEBBI any update here?
– Joakim Danielson
Jan 8 at 8:11
this is my jsonresponse
– Brahim CHEBBI
Jan 4 at 15:00
this is my jsonresponse
– Brahim CHEBBI
Jan 4 at 15:00
{ "items": [ { "id": "14", "unique_id": "5bb7682e2031f7.15799495", "name": "test", "email": "test@test.com", "encrypted_password": "5hQXusqX1GyyMO5ZvOrvJbkHvFMxOTQwYmYwM2Ew", "salt": "1940bf03a0", "created_at": "2018-10-05 15:33:34", "updated_at": null, "num_tel": "+21695502248", "image": "14", "adresse": "tunis-ariana", "DateNaissance": "12/14/2018" } ] }
– Brahim CHEBBI
Jan 4 at 15:00
{ "items": [ { "id": "14", "unique_id": "5bb7682e2031f7.15799495", "name": "test", "email": "test@test.com", "encrypted_password": "5hQXusqX1GyyMO5ZvOrvJbkHvFMxOTQwYmYwM2Ew", "salt": "1940bf03a0", "created_at": "2018-10-05 15:33:34", "updated_at": null, "num_tel": "+21695502248", "image": "14", "adresse": "tunis-ariana", "DateNaissance": "12/14/2018" } ] }
– Brahim CHEBBI
Jan 4 at 15:00
@BrahimCHEBBI any update here?
– Joakim Danielson
Jan 8 at 8:11
@BrahimCHEBBI any update here?
– Joakim Danielson
Jan 8 at 8:11
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54025572%2fcould-not-cast-value-of-type-nsarray0-to-nsdictionary%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
Well the error is pretty obvious isn't it?
let A = response.result.value as! Dictionary<String,Any>
. What does the json response look like?– Joakim Danielson
Jan 3 at 15:51
{ "items": [ { "id": "14", "unique_id": "5bb7682e2031f7.15799495", "name": "test", "email": "test@test.com", "encrypted_password": "5hQXusqX1GyyMO5ZvOrvJbkHvFMxOTQwYmYwM2Ew", "salt": "1940bf03a0", "created_at": "2018-10-05 15:33:34", "updated_at": null, "num_tel": "+21695502248", "image": "14", "adresse": "tunis-ariana", "DateNaissance": "12/14/2018" } ] }
– Brahim CHEBBI
Jan 3 at 15:53
1
So,
A["items"]
is an array that contains a dictionary rather than a dictionary itself. In general, don't use!
unless you really know what the correct type will be.– Phillip Mills
Jan 3 at 15:59
(Learn to) read the JSON. It's pretty easy. There are only 2 (two) collection types:
is array (Swift
[[String:Any]]
or[Dictionary<String,Any>]
),{}
is dictionary (Swift[String:Any]
orDictionary<String,Any>
).– vadian
Jan 3 at 16:22