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

前端之家收集整理的这篇文章主要介绍了ios – 无法将类型“__NSArrayM”(0x34df0900)的值转换为“NSDictionary”SWIFT前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
当从webservice解码 JSON响应时,我会收到一条错误
Could not cast value of type '__NSArrayM' (0x34df0900) to 'NSDictionary'

我尝试了在StackOverflow中找到的很多解决方案,但没有任何作用.

我的代码

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

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

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": {}
    }
]

感谢您的帮助

解决方法

尝试替换以下行:
let jsonData:NSDictionary = (NSJSONSerialization.JSONObjectWithData(urlData!,error: &error) as? NSDictionary)!

有以下几点:

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

我希望这将有助于您!

干杯!

猜你在找的iOS相关文章