将AnyObject以swift格式转换为Dictionary
我从AFNetworking的iTunes API获取数据,我想创build一个字典与响应,但我不能做到这一点。
错误:无法将expression式的types“Dictionary”转换为键入“Hashable”
这是我的代码:
func getItunesStore() { self.manager.GET( "https://itunes.apple.com/es/rss/topfreeapplications/limit=10/json", parameters: nil, success: { (operation: AFHTTPRequestOperation!,responseObject: AnyObject!) in var jsonResult: Dictionary = responseObject as Dictionary }, failure: { (operation: AFHTTPRequestOperation!,error: NSError!) in println("Error:" + error.localizedDescription) }) }
当你在Swift中定义一个Dictionary
,你也必须给键和值types。 就像是:
var jsonResult = responseObject as Dictionary<String, AnyObject>
但是,如果转换失败,则会出现运行时错误 – 您最好使用如下所示的代码:
if let jsonResult = responseObject as? Dictionary<String, AnyObject> { // do whatever with jsonResult }