Swift 3:从返回nil访问嵌套字典?

时间:2016-12-05 04:22:03

标签: ios swift dictionary swift3

我在尝试访问嵌套Dictionary时遇到问题,似乎返回nil

以下是info类型Dictionary<String, Any>的输出:

info = ["list_order": 1, "name": Some Text, "id": 1, "menu_items": {
    1 =     {
        "food_name" = "String";
        "food_picture" = "link";
        "food_price" = "2.00";
    };
    2 =     {
        "food_name" = "String";
        "food_picture" = "link";
        "food_price" = "5.00";
        id = 2;
    };
}]

info["menu_items"]的输出:

info["menu_items"] = {
    1 =     {
        "food_name" = "String";
        "food_picture" = "link";
        "food_price" = "2.00";
        id = 1;
    };
    2 =     {
        "food_name" = "String";
        "food_picture" = "link";
        "food_price" = "5.00";
        id = 2;
    };
}

但是,以下分配会在nil中生成test

let test = info["menu_items"] as? Dictionary<Int, Any>

是否有不明显的事情或我不理解基本原理?

0 个答案:

没有答案