srinivas
srinivas

Reputation: 5108

Data conversion from JSON results as NSDictionary[] throws EXC_BAD_INSTRUCTION runtime error

I have the following JSON being received in my Swift code, after being parsed as a NSDictionary. In my function I am trying to extract the JSON objects in "results" block as NSDIctionary[], but this is throwing a run-time error. I don't understand why, as this was working just a few days earlier.

{
"results": [
    {
        "id": "3",
        "name": "The National",
        "slug": "thenational",
        "facebook_url": "https://www.facebook.com/thenationalofficial/",
        "twitter_url": "https://twitter.com/The_National",
        "profile_image": "http://example.staging.com/media/profile_image/thumbnail_263x263/1352756032.jpg",
        "_type": "artist",
        "resource_uris": {
        }
    },
    {
        "id": "5",
        "name": "Mayer Hawthorne",
        "slug": "mayerhawthorne",
        "facebook_url": "https://www.facebook.com/MayerHawthorne",
        "twitter_url": "https://twitter.com/MayerHawthorne",
        "profile_image": "http://example.example.com/media/profile_image/thumbnail_263x263/1352755133.png",
        "_type": "artist",
        "resource_uris": {
        }
    },
    {
        "id": "20",
        "name": "I Play Maracas",
        "slug": "iplaymaracas",
        "facebook_url": "",
        "twitter_url": "",
        "profile_image": "http://staging.wedemand.com/images/en/img-list-home.gif",
        "_type": "artist",
        "resource_uris": {
            "_demanded_by": null,
            "demand_url": "http://ec2-54-86-17-163.compute-1.amazonaws.com/artists/20/?demand=1&access_token={}",
            "dismiss_url": "http://ec2-54-86-17-163.compute-1.amazonaws.com/artists/20/?demand=0&access_token={}"
        }
    },
    {
        "id": "35",
        "name": "Black SuperHeros",
        "slug": "blacksuperheros",
        "facebook_url": "",
        "twitter_url": "",
        "profile_image": "http://staging.example.com/images/en/img-list-home.gif",
        "_type": "artist",
        "resource_uris": {
        }
    },
    {
        "id": "49",
        "name": "Ayman Elgadi",
        "slug": "aymanelgadi",
        "facebook_url": "",
        "twitter_url": "",
        "profile_image": "http://staging.example.com/images/en/img-list-home.gif",
        "_type": "artist",
        "resource_uris": {
        }
    },
    {
        "id": "8874",
        "name": "Lauri",
        "slug": "lauri",
        "facebook_url": "http://www.facebook.com/hughlaurieblues",
        "twitter_url": "http://twitter.com/hughlaurieblues",
        "profile_image": "http://staging.example.com/media/profile_image/thumbnail_263x263/lauri_profilepic.jpg",
        "_type": "artist",
        "resource_uris": {
        }
    }
]
}

My IOS-Swift code receives the NSDictionary object after being parsed by AFNetworking lib and passes to the function which casts the results array as NSDictionary[], is now throwing run-time error, while earlier this was working.

(operation: AFHTTPRequestOperation!, responseObject: AnyObject!) in println("JSON: " + responseObject.description)

var jsonResult: NSDictionary = responseObject as NSDictionary

this jsonResult is passed to the function below which tries to cast as NSDictionary[]

let allResults: NSDictionary[] = results["results"] as NSDictionary[]

UPDATE: I printed the class of the results object as it is being returned as __NSCFDictionary.

Here What is an NSCFDictionary? is a discussion regarding this and says to use this just like NSDictionary, but in my case its not working.

Upvotes: 1

Views: 569

Answers (1)

ujell
ujell

Reputation: 2792

The value of results is not a dictionary in your JSON, it is an array. You should get it with something like this;

let allResults: NSArray = results["results"] as NSArray

Upvotes: 3

Related Questions