ios 无法将“__NSArrayM”(0x34df0900)类型的值转换为“NSDictionary”SWIFT

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30382520/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 06:02:58  来源:igfitidea点击:

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

iosjsonweb-servicesswiftnsdictionary

提问by f1rstsurf

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

从 webservice 解码 JSON 响应时,我收到一条错误消息:

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

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

我也尝试了在 StackOverflow 中找到的很多解决方案,但没有任何效果。

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:

来自 Web 服务的响应:

[
    {
        "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

感谢您的帮助

回答by Randika Vishman

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!

干杯!

回答by allbto

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

使用 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
}

回答by Francis Jervis

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)

如果您错过了读取日志的“级别”,就会发生这种情况。我遇到了这个错误,尝试转换为 NSArray 而不是 NSMutableDictionary 作为这里的 Swift JSON 错误,无法将类型 '__NSArrayM' (0x507b58) 的值转换为 '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)

对象的实际内容位于该数组索引 0 处的 NSDictionary 中。尝试使用此代码(包括一些日志行来说明)

        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)