如何在数组中获取字典对象

时间:2016-03-09 20:30:14

标签: ios swift

我有dict(NSDictionary),其中包含以下数据。

{
    images =     (
                {
            image = "image.jpg";
            scores =             (
                                {
                    "classifier_id" = "Adventure_Sport";
                    name = "Adventure_Sport";
                    score = "0.662678";
                },
                                {
                    "classifier_id" = Climbing;
                    name = Climbing;
                    score = "0.639987";
                },
                                {
                    "classifier_id" = Flower;
                    name = Flower;
                    score = "0.628092";
                },
                                {
                    "classifier_id" = "Nature_Scene";
                    name = "Nature_Scene";
                    score = "0.627548";
                },
                                {
                    "classifier_id" = Icicles;
                    name = Icicles;
                    score = "0.617094";
                },
                                {
                    "classifier_id" = Volcano;
                    name = Volcano;
                    score = "0.604928";
                },
                                {
                    "classifier_id" = Potatoes;
                    name = Potatoes;
                    score = "0.602799";
                },
                                {
                    "classifier_id" = "Engine_Room";
                    name = "Engine_Room";
                    score = "0.595812";
                },
                                {
                    "classifier_id" = Bobsledding;
                    name = Bobsledding;
                    score = "0.592521";
                },
                                {
                    "classifier_id" = White;
                    name = White;
                    score = "0.587923";
                },
                                {
                    "classifier_id" = Yellow;
                    name = Yellow;
                    score = "0.574398";
                },
                                {
                    "classifier_id" = "Natural_Activity";
                    name = "Natural_Activity";
                    score = "0.54574";
                },
                                {
                    "classifier_id" = Butterfly;
                    name = Butterfly;
                    score = "0.526803";
                },
                                {
                    "classifier_id" = "Dish_Washer";
                    name = "Dish_Washer";
                    score = "0.513662";
                },
                                {
                    "classifier_id" = Rainbow;
                    name = Rainbow;
                    score = "0.511032";
                }
            );
        }
    );
}

我无法找到一种方法来访问数组中的classfier_id

真的需要你的帮助。谢谢。 PS我已经尝试dict["scores"]dict["image.scores"]

请帮助..谢谢

2 个答案:

答案 0 :(得分:1)

你想要

let classifier_ids = dict["scores"].map{$0["classifier_id"]}

如果你的容器是NSObjects而不是Swift Dictionaries和Arrays,那么你需要添加一些类型转换,但这是基本的想法。

此代码对于非类型集合更安全:

var classifier_ids: [String?]
if let array = dict["scores"] as? [String:String]
{
  let classifier_ids = array.map{$0["classifier_id"]}
}

这应该为您提供一个选项数组,如果数组中的条目不包含“classifier_id”键/值对,则给定条目将为nil。

或者如果您想跳过不包含“classifier_id”键/值对的条目:

var classifier_ids: [String]
if let array = dict["scores"] as? [String:String]
{
  let classifier_ids = array.flatmap{$0["classifier_id"]}
}

答案 1 :(得分:1)

在时间迈出一步:

if let array = dict["images"] as? NSArray {
    if let array2 = array[0]["scores"] as? NSArray {
        if let ids = array2.valueForKey("classifier_id") as? [String] {
            // use array of ids
            print(ids)
        }
    }
}

或一次拍摄:

if let ids = (dict["images"]?[0]["scores"])?.valueForKey("classifier_id") as? [String] {
    // use array of ids
    print(ids)
}