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 trialtytyty
4,974 Pointsquestion about syntax for the Treehouse model
We were given this code with this 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.
Once you have the image name, create an instance of UIImage using the image name and store it in a constant called profilePicture.
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let friends = Treehouse().friends
//add your code here
}
}
I successfully completed the challenge with:
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let friends = Treehouse().friends
let profileImageName = friends["profilePicture"] as String!
let profilePicture = UIImage(named: profileImageName)
}
}
But isn't it confusing syntax to use the same name with the library and the constant? He wrote: let friends = Treehouse().friends. Shouldn't one of them be capitalized or have a different name?
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let friends = Treehouse().friends
//Add your code below
}
}
struct Treehouse {
let friends = [
"firstName": "Susan",
"lastName": "Olson",
"profilePicture": "susan_profile.png"
]
}
Patrick Cooney
12,216 PointsHaha. Calm down man. I have a feeling one of the mods updated the code for you. (If that's the case they should have made a note that they edited your post though.)
2 Answers
Chris Shaw
26,676 PointsHi tymac,
The naming conventions used are very common and in this particular instance it makes perfect sense to store the dictionary of Treehouse().friends
in a friends
constant as it gives context to the rest of the code that follows.
Shouldn't one of them be capitalized or have a different name?
Simple answer, no. Starting something with a capital letter should only occur when creating an struct, class, enum etc.
Hope that helps.
tytyty
4,974 PointsThanks Patrick and Chris.
Regarding the weird code Treehouse added. Actually this isn't the first time some mysterious code has appeared. I didn't put that Treehouse.swift code there and it showed up as soon as I clicked post. It's almost like Treehouse has some bot that looks for code from challenge Tasks. The Treehouse bot sees it and adds some code that expands on the code from the challenge. See where it gives first name, last name, and profile picture in the Treehouse.swift? I didn't put that there. And oddly enough it fits with the imaginary app in the challenge. In my code I added a constant called profilePicture. Well Treehouse bot added a friends dictionary. What the heck????????
Chris Shaw
26,676 PointsRegarding the weird code Treehouse added. Actually this isn't the first time some mysterious code has appeared. I didn't put that Treehouse.swift code there and it showed up as soon as I clicked post. It's almost like Treehouse has some bot that looks for code from challenge Tasks.
This is a new feature that was added in December to help users with code challenges whom didn't know how to post code here on the forum, when you're on a challenge and click the Ask A Question button a checkbox is underneath the area where you type in your question which says the following.
Attach my code to this post
If this box is checked your question along with the challenge code will get posted which means you no longer need to post the code yourself.
The code you have put in your post is yours, anything with .swift is code posted from the challenge so if you've passed the challenge, gone back and then asked a question without changing anything you will see two different blocks of code instead of two of the same.
Hope that helps to clear things up as this is just an automated process when posting questions, there isn't any bots involved in adding this code after your question is posted.
Happy coding!
tytyty
4,974 PointsYeah new feature, got it. But it automatically posted code that was not in the challenge. It's actually great code because it lets me see real-world the code that could have existed with the code in the challenge. Again, this code you see in my post here was not in the challenge however it does complement the challenge. Maybe you guys could have a button in the challenges that when clicked lets us see this, I'll call it "finishing code". If I never created this question I would've never seen it. Cheers.
struct Treehouse {
let friends = [
"firstName": "Susan",
"lastName": "Olson",
"profilePicture": "susan_profile.png"
]
}
tytyty
4,974 Pointstytyty
4,974 PointsWoah excuse me, where did all this coloured code come from? And what is the Treehouse struct code doing here? I never put it here and it wasn't in the challenge!!! Freaky stuff man!!! My question ends at my last text comment. I don't know what the stuff below it is from.