Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community!

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial


How to get values for A and B in dictionary [A : B] while iterating through a For loop?

Hi, I need to retrieve this dictionary's key (image) and value (thumbnail): [image : thumbnail]. I will be looping through a collection of these key-value pairs.

My goal for this code is: when you click on a thumbnail, you go to its corresponding full-size image. I'm using a collectionView to present the thumbnails. The reason I'm trying a dictionary is: I had retrieved the thumbnails and full images in separate arrays, but the problem was, I'd click on the thumbnail and it wouldn't bring me to the corresponding image. This is because arrays do not keep order.

So I'm trying to get an ordered collection of images : thumbnails, and then take from those the image part and the thumbnail part, and then be able to use those each in a collectionView, where clicking on the thumb brings me to the image.

Please help.

Brian Steele
Brian Steele
23,060 Points
for (image, thumbnail) in myDictionary {
    //do things with your image and thumbnail

This should get you iterating though the key/value pairs

4 Answers

Thanks for the reply, Brian. That looks good, but there is another thing---when using Parse (which I'm doing), I have to call this method (getDataInBackgroundWithBlock)... Do I separate the two PFFiles using the syntax you mentioned above, then call getData(...) on each, then put them back into a dictionary as UIImages? This big problem is: I only know how to populate a CollectionView from an array... How do I populate a CollectionView from a dictionary? With the thumb index the same as the full-size index, how do I pass the full-size to the destinationViewController? (It's all a bit complicated).

Edit: I tried the syntax you mentioned, but it seems to only do one iteration. I have three objects in the images Dictionary, but when I run them through the "for in" you mentioned, only one object is put into the new Dictionary.

Example: I have ["A" : "B", "C" : "D", "E" : "F"], and when I use your for-in (converting the PFFiles to UIImages...) and then count my new dictionary, all I have is ["AImage", "BImage"]. Thus, it seems... Ah, wait... Here might be an issue: I use the for (image, thumbnail) in imageDict ... then I have two separate {do stuff} {do stuff} parts, for the getDataInBackgroundWithBlock (one for image, one for thumbnail), then, below their enclosures, I use parseImageDictionary.updateValue(imageParse, forKey: thumbParse)... but I'm thinking here's a problem. If the .updateValue only is called once (when the two {do stuff} {do stuff}s are done), that could be why only one object is showing up in the dictionary. Do you see the complex problem here? I need the two images together, but I need to separate them to getData out of them... I don't think Parse has a getData for dictionary PFFiles... Right? Or do they? That would make it simpler.

To reiterate: I have a Dictionary of PFFiles, I need to get the UIImages from them, but they need to return to Dictionary because they need to have same index, so I can use them in a thumbnail / full size CollectionView situation.

The mind-blowing thing is... THIS MUST BE A COMMON BIT OF CODE! The internet lets me down sometimes. This must be a super common situation. You need thumbnails because they fit better in little boxes that you click with your thumbs. But you need the full-size too, because they look better. But as arrays, the two will never line up (un-ordered), so you can't use their indexes.

Any idea what's up? Thanks.

I've been messing with this a while now, and I'm second guessing using a Dictionary for this. How do you get two images out of a Dictionary?

[Key: Image, Value: ThumbnailImage] ... I need both, not just one.