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

Cillian Warfield
Cillian Warfield
15,020 Points

having trouble with creating UUIImageview instance task, please help!

I'm having trouble completing this task re creating uiimageview instances. I guess the main problem is that I don't fully understand what I'm trying to accomplish. I've watched the previous a couple of times too.... Some explanation/code appreciated. Thanks

ImageViews.swift
import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        let friends = Treehouse().friends
        let profileImageName = Treehouse().friends.profilePicture
        let profilePicture = UUImage(named: profileImageName)

    }

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

Hi, I seemed to have figured this code out. But I'm still not 100% sure what's going on, so anyone, feel free to explain! Cheers

 let friends = Treehouse().friends
        let profileImageName = friends["profilePicture"] as String!
        let profilePicture = UIImage(named: profileImageName)

1 Answer

Hello Cillian,

In your first code block for this challenge, here is you need to write instead:

let profileImageName = friends["profilePicture"] as String!
let profilePicture = UIImage(imageNamed: profileImageName)

You did a great job on this task, but you forgot two vital things.

First, you used you misspelled the method name for UIImage, it should be imagedNamed:. And secondly, you forgot to unwrap the value that you received from the dictionary as a string. You can unwrap a string using the as keyword and the value that you want to cast followed by a bang (!).

For the explanation,

You need to unwrap the value because the value that you receive using the dictionary key could be a float, int, string, or any other kind of object that is stored for that key.

For example, here is a example (not real) that could be used to return all Treehouse Forum Answers that I have answered from the dictionary that contains them all. This object at the key is going to be an array of string so we need to cast it to that.

let myTreehouseResponse = treehouseResponses["LandonFerrier"] as [String]

If you have any more questions, feel free to ask me or check out the teachers notes under the videos!

Cillian Warfield
Cillian Warfield
15,020 Points

Thanks for taking the time out to answer and explain. It's beginning to make some sense (to me)!