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
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
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With