Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Could not cast value of type '__NSArrayM' (0x34df0900) to 'NSDictionary' SWIFT

When decoding JSON response from webservice I get an error saying:

Could not cast value of type '__NSArrayM' (0x34df0900) to 'NSDictionary'

I tried out so many solutions found in StackOverflow too, but nothing works.

My Code :

let jsonData:NSDictionary = (NSJSONSerialization.JSONObjectWithData(urlData!, options:NSJSONReadingOptions.MutableContainers , error: &error) as? NSDictionary)!

let success:NSInteger = jsonData.valueForKey("success") as! NSInteger

Response from the Web Service:

[
    {
        "id": "1",
        "title": "bmw",
        "price": "500.00",
        "description": "330",
        "addedDate": "2015-05-18 00:00:00",
        "user_id": "1",
        "user_name": "CANOVAS",
        "user_zipCode": "32767",
        "category_id": "1",
        "category_label": "VEHICULES",
        "subcategory_id": "2",
        "subcategory_label": "Motos",
        "bdd": {}
    }
]

Thank you for your help

like image 688
f1rstsurf Avatar asked May 21 '15 19:05

f1rstsurf


1 Answers

Use SwiftyJSON : https://github.com/SwiftyJSON/SwiftyJSON

let json = JSON(data: urlData!)

And if success is in the array

if let success = json[0]["success"].int {
     //Now you got your value
}

Or if success is not in the array

if let success = json["success"].int {
     //Now you got your value
}

You can also check success value

if let success = json["success"].int where success == 1 {
     // Now you can do stuff
}
like image 59
allbto Avatar answered Sep 28 '22 03:09

allbto