当从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)!
我希望这将有助于您!
干杯!