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

Adam Bridges
Adam Bridges
1,904 Points

Building a Music Library

Please help me complete the following challenge:

In the ViewController class, using the key profilePicture, retrieve the image name as a string from the dictionary and assign it to a constant named profileImageName. I’ve already created an instance of the Treehouse struct and stored a reference to the friends library in a constant named friends for you to use.

I don't understand how to code the above instructions. Help!

1 Answer

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        let friends = Treehouse().friends
        let profileImageName = friends["profilePicture"]
        //Add your code below
        let profilePicture = profileImageName! 

The challenge has already created an instance of the Treehouse struct for you by assigning it to the constant 'friends.' As part of this, it's also called the dictionary 'friends' property of Treehouse. So if you want to assign a specific key to profileImageName, you need to access the "profilePicture" key in friends, since friends is an instance of Treehouse, right?

Once you've got the key assigned to profileImageName, you need to unwrap it with the ! operator and assign it to the profilePicture constant. That successfully accesses to value from the key:value bundle and creates a UIImage constant, profilePicture.