我遇到一个错误,提示Cast from '[String : AnyObject]' to unrelated type '[[String : AnyObject]]' always fails
for dict in json as! [[String: AnyObject]] {
let VideoCategory = videoCategory()
VideoCategory.setValuesForKeys(dict)
videoCategories.append(VideoCategory)
}
我尝试将其包装在if语句中,但仍然显示错误
我在做什么错了?
这是JSON feed
{
"action":"videos",
"result": {
"count":2,"videos":
[
{
"id":"427","asset_id":"279804413","asset_title":"title_1"
}
{
"id":"428","asset_id":"279804413","asset_title":"title_2"
}
]
}
答案 0 :(得分:1)
我认为我做对了。我首先将其声明为字符串对象。是 这是一个好方法吗?
let videos = json["result"] as! [String:AnyObject]
for dict in videos["videos"] as! [[String: AnyObject]] {
let VideoCategory = videoCategory()
VideoCategory.setValuesForKeys(dict)
videoCategories.append(VideoCategory)
}
答案 1 :(得分:-3)
请尝试如下使用json,根据我的理解,json是字典的数组,因此您需要添加 json! [[String:AnyObject]] ,以便编译器可以理解您所写的内容。请检查以下代码
for dict in (json as! [[String: AnyObject]]) {
let VideoCategory = videoCategory()
VideoCategory.setValuesForKeys(dict)
videoCategories.append(VideoCategory)
}