我从iTunes API与AFNetworking获取数据,我想创建一个字典与响应,但我不能这样做。
错误:无法将表达式的类型“字典”转换为类型“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中定义一个字典时,你还必须给出键和值类型。就像是:
var jsonResult = responseObject as Dictionary<String,AnyObject>
如果转换失败,但是,你会得到一个运行时错误 – 你最好的像:
if let jsonResult = responseObject as? Dictionary<String,AnyObject> { // do whatever with jsonResult }