我有一本字典。当我尝试访问该字典时,它应该返回一个空值,当它应该肯定返回一个实际值。我很难过。任何帮助总是受到赞赏。
if let d = response.result.value {
print(d) //prints correct data
let prices = d["prices"] as? [[String:AnyObject?]]
print(prices) //prints nil
let best_price = prices?[0]
let price = best_price?["price"] as? String
print(price) //prints nil
}
控制台:
{
item = {
"item_number" = 57;
image = "http://example.com/tent.jpg";
name = "Small Red Tent";
};
prices = (
{
link = "http://example.com/id=19";
price = "58.15";
rating = "3.64";
"vendor_id" = 50;
},
{
link = "http://example.com/id=50";
price = "58.14";
rating = "5.00";
"vendor_id" = 110;
},
{
link = "http://example.com/id=26";
price = "50.40";
rating = "4.71";
"vendor_id" = 73;
},
{
link = "http://example.com/id=12";
price = "47.16";
rating = "4.00";
"vendor_id" = 1;
},
{
link = "http://example.com/id=13";
price = "45.75";
rating = "3.90";
"vendor_id" = 25;
},
{
link = "http://example.com/id=16";
price = "41.32";
rating = "3.02";
"vendor_id" = 16;
},
{
link = "http://example.com/id=1";
price = "36.59";
rating = "4.84";
"vendor_id" = 51;
},
{
link = "http://example.com/id=2";
price = "36.29";
rating = "3.26";
"vendor_id" = 43;
},
{
link = "http://example.com/id=13";
price = "34.59";
rating = "4.14";
"vendor_id" = 48;
},
{
link = "http://example.com/id=3";
price = "32.00";
rating = "4.29";
"vendor_id" = 53;
},
{
link = "http://example.com/id=4";
price = "24.50";
rating = "4.16";
"vendor_id" = 8;
},
{
link = "http://example.com/id=5";
price = "15.00";
rating = "4.87";
"vendor_id" = 39;
},
{
link = "http://example.com/id=6";
price = "0.00";
rating = "3.00";
"vendor_id" = 65;
}
);
}
nil
nil
答案 0 :(得分:0)
So the solution was proposed by Larme solved the problem.
let prices = d["prices"] as? [AnyObject]
Although I never figured out why casting d["prices"] as? [[String:AnyObject?]]
wouldn't also work.