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! While you're at it, check out some resources Treehouse students have shared here.

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

iOS Build a Playlist Browser with Swift Building the Music Library and Playlist Models Creating UIImageView instances

Ricardo Gonzalez
Ricardo Gonzalez
2,286 Points

How am i supposed to do this problem?

How can i retrieve the image like it's asking me to do?

ImageViews.swift
import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        let friends = Treehouse().friends

        UIImage


    }

}
Treehouse.swift
struct Treehouse {
    let friends = [
        "firstName": "Susan",
        "lastName": "Olson",
        "profilePicture": "susan_profile.png"
    ]
}

1 Answer

Hi there,

There are two parts to this challenge. First is getting the file name out of the dictionary. The dictionary is called friends and the key for the image file name is called profilePicture. So we need to assign the result of using that key to a constant called profileImageName as the question tells us:

        let profileImageName = friends["profilePicture"]!

(Don't forget the ! to unwrap the optional)

Next we want to create a UIImage using this filename and store the resulting image in a constant called profilePicture. UIImage has a useful method called imageNamed: that works well for these scenarios:

        let profilePicture = [UIImage imageNamed: profileImageName]

Job done!

The whole function looks like:

    override func viewDidLoad() {
        super.viewDidLoad()
        let friends = Treehouse().friends
        //Add your code below
        let profileImageName = friends["profilePicture"]!
        let profilePicture = [UIImage imageNamed: profileImageName]
    }

I hope that makes sense.

Steve.