13

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

Randika Vishman
  • 7,983
  • 3
  • 57
  • 80
f1rstsurf
  • 637
  • 1
  • 6
  • 22

3 Answers3

12

Try replacing following line:

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

With following:

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

I hope this will help you!

Cheers!

Randika Vishman
  • 7,983
  • 3
  • 57
  • 80
  • i'm, not in front of my mac until tonight, i'll tell you that tonight ! Thank you for your message ! – f1rstsurf May 22 '15 at 06:46
  • now i have : "Binary operator '!=' cannot be applied to operands of type 'NSArray?' " and 'nil' for "if jsonData["message"] as? NSArray != nil {" – f1rstsurf May 22 '15 at 18:16
  • how about using something like `NSArray.count > 0` like thing to check if it is not empty or empty? – Randika Vishman May 23 '15 at 03:11
  • i already tried this solution but i got "Count is not a member of type NSArray " :( – f1rstsurf May 23 '15 at 11:37
  • Actually these comments aren't relevant to the Q&A thread you have created! Anyway you better look these two stackOverflow answers! `answer1 :http://stackoverflow.com/questions/27141784/swift-nsarray-does-not-have-member-named-count` `And Answer2 : http://stackoverflow.com/questions/25540208/optional-in-swift-return-count-of-array` This is all I can help you regarding this, sorry! Specially I don't know much about Swift! – Randika Vishman May 23 '15 at 14:19
  • i finally found where the problem was, in fact in my webservice i have some control, which check if some fields are empty, and if they are, i return 0 with a error message, if not i return 1, i just realised i forgot to put 1 or 0 in my control, and so when i retieve my json datas, in xcode, my webservice send nil value for $response. By the way the app crash. Hope that will be helpfull for someone else. Thank for your help. – f1rstsurf May 24 '15 at 09:38
5

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
}
allbto
  • 169
  • 1
  • 4
  • 15
3

This will happen if you miss a "level" reading the logs. I was encountering this error, tried casting to an NSArray instead of NSMutableDictionary as here Swift JSON error, Could not cast value of type '__NSArrayM' (0x507b58) to 'NSDictionary' (0x507d74)

The actual contents of the object were inside an NSDictionary at index 0 of that array. Try with this code (including some log lines to illustrate)

        let dataDict = NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.MutableContainers, error: &error)

        println(dataDict)

        let contents = dataDict!.objectForKey("rows") as! NSMutableArray

        println(contents)

        println( "contents is = \(_stdlib_getDemangledTypeName(contents))")

        let innerContents = contents[0]

        println(innerContents)

        println( "inner contents is = \(_stdlib_getDemangledTypeName(innerContents))")

        let yourKey = innerContents.objectForKey("yourKey") as? String
        println(yourKey)