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 trialCillian Warfield
15,020 Pointshaving 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
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let friends = Treehouse().friends
let profileImageName = Treehouse().friends.profilePicture
let profilePicture = UUImage(named: profileImageName)
}
}
struct Treehouse {
let friends = [
"firstName": "Susan",
"lastName": "Olson",
"profilePicture": "susan_profile.png"
]
}
1 Answer
landonferrier
25,097 PointsHello 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
15,020 PointsThanks for taking the time out to answer and explain. It's beginning to make some sense (to me)!
Cillian Warfield
15,020 PointsCillian Warfield
15,020 PointsHi, 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