Failed to pass value like "__NSArrayI" (0x10df73c08) to "NSDictionary" (0x10df74108)

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 another array like task_id (array), but I get this error:

"Failed to distinguish value of type __NSArrayI from NSDictionary"

Please help me

thanks in advance

+5
source share
2 answers

Your answer has an Array Dictionary not Dictionary , so if you want the entire dictionary you can access it with a 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) } 
+9
source

This is because you are returning the results in an array form. What you need to do is cast the result in NSArray. Then you can access individual results by index:

 let JSON = result as! NSArray let firstResult = results[0] 
+2
source

Source: https://habr.com/ru/post/1259396/


All Articles