访问 NSDictionary 中的信息

Accessing information within a NSDictionary

我正在尝试解析 NSDictionary 中的数据,但遇到了问题。每当我尝试访问此信息时,它都会打印 nil。

NSDictionary:

{
"-LTR7P8PFWHogjlBENiJ" =     {
    filmid = 335983;
    rating = "5.5";
    review = Ehh;
};
}

代码:

let dict = info.value as! NSDictionary
print(dict) //prints about NSDict
print(dict["review"] as? String) //prints nil

从 NSDictionary 打印 "Ehh" 的正确方法是什么?

您有搜索结果的外部字典包装器,带有字符串键 -LTR7P8PFWHogjlBENiJ

因此您的代码当前在外部字典中搜索键 review

你可以得到下面的review值,

typealias JSON = [String: Any]
let dictionary: JSON = [
    "-LTR7P8PFWHogjlBENiJ": [
        "filmid": 335983,
        "rating": "5.5",
        "review": "Ehh"
    ]
]

if let reivew = (dictionary.first?.value as? JSON)?["review"] as? String {
    print(reivew)
}

对于 NSDictionary,您可能必须按如下方式使用它,

let dict = info.value as! NSDictionary
if let reivew = (dict.allValues.first as? JSON)?["review"] as? String {
    print(reivew)
}