Could not cast value of type '__NSArrayI' (0x10df73c08) to 'NSDictionary' (0x10df74108)
22,048
Solution 1
Your response is of Array
of Dictionary
not Dictionary
, so if you want all the dictionary you can access it using loop.
if let array = response.result.value as? [[String: Any]] {
//If you want array of task id you can try like
let taskArray = array.flatMap { $0["task_id"] as? String }
print(taskArray)
}
Solution 2
That's because you service returns results in form of an array. What you need to do is to cast result to NSArray. Then you can access separate results by index:
let JSON = result as! NSArray
let firstResult = results[0]
Solution 3
Swift 4.1 +
if let resultArray = jsonResponse.result.value as? [[String: Any]] {
let taskArray = resultArray.compactMap { $0["task_id"] as? String }
}
Comments
-
seggy almost 2 years
let url = "http://xyz/index_main.php?c=webservices&a=get&e=007" Alamofire.request(url, method: .post, parameters: nil, encoding: JSONEncoding.default) .responseJSON { response in print(response) //to get status code if let status = response.response?.statusCode { switch(status){ case 201: print("example success") default: print("error with response status: \(status)") } } //to get JSON return value if let result = response.result.value { let JSON = result as! NSDictionary print(JSON) }
Result :
{ "create_by" = 007; "create_date" = "2016/06/20"; "due_date" = "2016/06/22"; "estimate_time" = ""; fixer = 007; priority = High; "project_code" = testing; status = "Re-Open"; "task_id" = 228; "task_title" = test; tester = 007; }, { "create_by" = 006; "create_date" = "2016/06/23"; "due_date" = "2016/06/24"; "estimate_time" = ""; fixer = 007; priority = Critical; "project_code" = "tanteida.c"; status = "In Progress"; "task_id" = 234; "task_title" = testing; tester = 006; }
I want to convert to
NSDictionary
and get different Array liketask_id
(array) but I get this error:"Could not cast value of type '__NSArrayI' to NSDictionary"
Please Help me
Thank you in advance
-
seggy over 7 years1 more que can i use taskArray as global varialbe?
-
Nirav D over 7 yearsFor that you need to declare taskArray as instance variable of your Controller.
-
seggy over 7 yearsi m new in swift so please tell me how i declare like that var taskArray = Array<any>() but i know this is not right
-
Nirav D over 7 yearsLike this way
var taskArray = [String]()
-
seggy about 7 yearsHey Nirav hope you help me, in taskArray sometime get null value from web service that time how to handle?.
-
Nirav D about 7 years@seggy Then use
flatMap
instead ofmap
it will ignore the nil and give you the array ofString
notString?
. -
seggy about 7 yearsi also used flat map but when null value comes than does't work BTW i got the solution array.map{($0["task_id"] as? String) ?? ""} thank you
-
Nirav D about 7 years@seggy
flatMap
will ignore the nil so you get less object when it is nil and currently the way you are using that will give you empty string if it is nil. -
seggy about 7 yearsyes you are right i was get error(cast error) because i am doing like this msg.flatMap{($0["description"] as? String)!} now i got it
-
Nirav D about 7 years@seggy Happy to help :)