Swift 从返回 null 的字典访问键

Swift accessing key from dictionary returning null

我有一本字典。当我尝试访问该字典时,它返回一个空值,而它应该返回一个实际值。我很难过。任何帮助总是感激。

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

所以Larme提出的解决方案解决了这个问题。

let prices = d["prices"] as? [AnyObject]

尽管我一直不明白为什么转换 d["prices"] as? [[String:AnyObject?]] 也不起作用。