user3766930 user3766930 - 1 year ago 120
Swift Question

After casting json data to [[String: AnyObject]] it seems to be empty - why?

I'm calling my webservice with

and trying to parse the json result.

My code looks like this:

.responseJSON { response in
switch response.result {
case .success:
DispatchQueue.main.async(execute: {

if let jsonData = response.result.value as? [[String: AnyObject]] {
print("this is not printed")

The first print returns:

hashtags = (

and the 2nd one is not printed at all - the code is never executed. Why?

When I call my webservice in the browser I'm getting:


Answer Source

The JSON structure you are trying to convert is a dictionary not an array. You are trying to convert the response to an array of dictionary and that's why it is failing.

You need to use:

if let jsonData = response.result.value as? [String: AnyObject]
   // Handle data
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download