How to resolve un-ordered JSON dictionary retrieved from YouTube Api?
<p>I've been stuck on this for a while and cannot think logically how to resolve this, and would like some help.</p>

<p>Basically, I'm using the YouTube api to fetch all the video IDs from a changel. I store each of these IDs in an array, and I loop through its indices, each time calling this: <a href="https://developers.google.com/youtube/v3/docs/videos/list" rel="nofollow">https://developers.google.com/youtube/v3/docs/videos/list</a></p>

<p>This allows me to get the video duration of each video, where I put it in another array of AnyObjects and convert it to a recognizable format.</p>

<p>Here's the output of my videoIDs array, as I fetch it in groups of 10:</p>

<pre><code>videoIDs = ["173ZNVycdQo", "NztC0z1JNq0", "U2C81DNHw2M", "09XKD2sS60E", 
            "Wc0cVjv44Xc", "ENKHANzmeh4", "mIW5trLZJcM", "KW0ehzTVo-s", 
            "1MyVzWIwFs4", "HyTQBpZeJCc"]
</code></pre>

<p>The problem is in the for loop as shown here:</p>

<pre><code>for (var j = 0; j &lt; self.videoIDs.count; j++)
{
    self.getDurations("https://www.googleapis.com/youtube/v3/videos?part=snippet,contentDetails,statistics,status&amp;id=\(self.videoIDs[j])&amp;key=\(self.apiKey)")
}
</code></pre>

<p>And Here's my getDurations function:</p>

<pre><code>func getDurations(urlString: String)
{
    let targetURL = NSURL(string: urlString)

    performGetRequest(targetURL, completion: { (data, HTTPStatusCode, error) -&gt; Void in

        if HTTPStatusCode == 200 &amp;&amp; error == nil
        {
            do
            {
                self.resultsVideoDurationsDict = try NSJSONSerialization.JSONObjectWithData(data!, options: []) as! Dictionary&lt;NSObject, AnyObject&gt;

                print("resultsDict = \(self.resultsVideoDurationsDict)")

                let items = self.resultsVideoDurationsDict["items"] as AnyObject!

                // Loop through all items and add it to another dictionary
                for (var i = 0; i &lt; items.count; i++)
                {
                    self.unformattedDurations.append(items[i])
                }
            }
            catch
            {
                print(error)
            }
        }
        else
        {
            print("HTTP Status Code = \(HTTPStatusCode)")
            print("Error while loading channel details: \(error)")
        }
    })
}
</code></pre>

<p>At every iteration, the JSON of <code>self.resultsVideoDurationsDict</code> is different, i.e. the order of the video IDs within the <code>videoIDs</code> array does not correspond to the same order of data output from the JSON.</p>

<p>I know that due to time delays in the NSURLSession, some data may be fetched earlier than others.</p>

<p>Is there a way to remedy this to way to ensure my <code>self.unformattedDurations</code> contains the ordered data corresponding to the data in the <code>videoIDs</code> array?</p>

<p>Thanks.</p>

<p><strong>UPDATE:</strong></p>

<p>Here's one possible output of <code>self.resultsVideoDurationsDict</code>:</p>

<pre><code>resultsDict = [etag: "DsOZ7qVJA4mxdTxZeNzis6uE6ck/EfACrkkmn5_QpCjD89FgcTdjxVY", kind: youtube#videoListResponse, items: (
        {
        contentDetails =         {
            caption = false;
            definition = hd;
            dimension = 2d;
            duration = PT11M29S;
            licensedContent = 0;
        };
        etag = "\"DsOZ7qVJA4mxdTxZeNzis6uE6ck/7wjJ90GVQP7Bo9GwdX8LfdJ8jdg\"";
        id = HyTQBpZeJCc;
        kind = "youtube#video";
        snippet =         {
            categoryId = 22;
            channelId = UC5ltMmeC4YFaart1SSXdmAg;
            channelTitle = "Viet My Television";
            description = "";
            liveBroadcastContent = none;
            localized =             {
                description = "";
                title = "PSCD TIEC QUAN 4";
            };
            publishedAt = "2016-02-02T17:31:58.000Z";
            thumbnails =             {
                default =                 {
                    height = 90;
                    url = "https://i.ytimg.com/vi/HyTQBpZeJCc/default.jpg";
                    width = 120;
                };
                high =                 {
                    height = 360;
                    url = "https://i.ytimg.com/vi/HyTQBpZeJCc/hqdefault.jpg";
                    width = 480;
                };
                maxres =                 {
                    height = 720;
                    url = "https://i.ytimg.com/vi/HyTQBpZeJCc/maxresdefault.jpg";
                    width = 1280;
                };
                medium =                 {
                    height = 180;
                    url = "https://i.ytimg.com/vi/HyTQBpZeJCc/mqdefault.jpg";
                    width = 320;
                };
                standard =                 {
                    height = 480;
                    url = "https://i.ytimg.com/vi/HyTQBpZeJCc/sddefault.jpg";
                    width = 640;
                };
            };
            title = "PSCD TIEC QUAN 4";
        };
        statistics =         {
            commentCount = 0;
            dislikeCount = 0;
            favoriteCount = 0;
            likeCount = 0;
            viewCount = 14;
        };
        status =         {
            embeddable = 1;
            license = youtube;
            privacyStatus = public;
            publicStatsViewable = 1;
            uploadStatus = processed;
        };
    }
), pageInfo: {
    resultsPerPage = 1;
    totalResults = 1;
}]
</code></pre>

<p>The id of that result is <strong>HyTQBpZeJCc</strong>, which corresponds to the 9th index of the <code>videoIDs</code> array, not the first index. The first output should be for video ID <strong>173ZNVycdQo</strong>. Therefore, different outputs are produced at runtime. </p>