Ios swift2可能存在拆封问题?

Ios swift2可能存在拆封问题?,ios,xcode,swift,Ios,Xcode,Swift,这是我的代码截图,我希望这比粘贴它更好,这样你就可以准确地看到xcode告诉我的内容以及在哪一行。我试着打开包装!在它上面的线上不同的地方,但我似乎不知道我没有正确地解开什么。我还在学习swift2,所以如果我错过了一些简单的东西,我很抱歉 编辑:如果不更改为我生成的错误。此外,我还以粘贴的形式附加代码 jsonResults[items]可能存在也可能不存在,因此其类型为可选的AnyObject 要进一步索引,可以使用可选的链接,如 if let item = jsonResults["ite

这是我的代码截图,我希望这比粘贴它更好,这样你就可以准确地看到xcode告诉我的内容以及在哪一行。我试着打开包装!在它上面的线上不同的地方,但我似乎不知道我没有正确地解开什么。我还在学习swift2,所以如果我错过了一些简单的东西,我很抱歉

编辑:如果不更改为我生成的错误。此外,我还以粘贴的形式附加代码

jsonResults[items]可能存在也可能不存在,因此其类型为可选的AnyObject

要进一步索引,可以使用可选的链接,如

if let item = jsonResults["items"]?[0] as? NSDictionary ...

有关更多详细信息,请参阅我的答案。

如果您使用Swift 2.0和Xcode 7,我们可以使用guard 检查以下代码:

 guard let item = jsonResults["items"]?[0] as? NSDictionary ... 
 else {   return    }

看起来您正在尝试为jsonResult[items]下标,但它是AnyObject?。不能下标,因为它不是集合、列表或序列。

不过,您可以为数组下标

如果您希望jsonResult[items]是字典的数组

jsonResult["items"] as? [NSDictionary] 

例子

if let items = jsonResult["items"] as? [NSDictionary?], item = items[0] {
    print(item)
}

if let items = jsonResult["items"] as? [[String: AnyObject]?], item = items[0] {
    print(item)
}

guard let item = jsonResult["items"] as? [[String: AnyObject]?], item = items[0] {
    return
}
print(item)

与其发布图片,不如发布一些代码和错误。。。如果让item=jsonResult[items][0]as,那么您的JSON看起来像是我将其更改为[code]的可能副本吗?NSDictionary{printitem}[/code]但它仍然是你错过的同一个错误?根据我的建议。事实上,我很抱歉…我错过了?你在[items]之后有了,而这正是我所缺少的。非常感谢。
jsonResult["items"] as? [[String: AnyObject]]
if let items = jsonResult["items"] as? [NSDictionary?], item = items[0] {
    print(item)
}

if let items = jsonResult["items"] as? [[String: AnyObject]?], item = items[0] {
    print(item)
}

guard let item = jsonResult["items"] as? [[String: AnyObject]?], item = items[0] {
    return
}
print(item)