简体   繁体   中英

How to get dictionary objects in array

I have a dict(NSDictionary) that has the following data.

{
    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";
                }
            );
        }
    );
}

I am not able to figure out a way to access classfier_id in an array

Really need your help. Thanks. PS I have already tried dict["scores"] and dict["image.scores"]

Kindly help.. Thanks

You want

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

If your containers are NSObjects rather than Swift Dictionaries and Arrays then you will need to add some type-casting, but that's the basic idea.

This code would be safer for non-typed collections:

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

That should give you an array of optionals, where a given entry will be nil if that entry in the array did not contain a "classifier_id" key/value pair.

Or if you want to skip entries that don't contain a "classifier_id" key/value pair:

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

Taking it a step at at time:

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)
        }
    }
}

or all in one shot:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM