Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

iOS Enumerations and Optionals in Swift Introduction to Optionals Initializing Optional Values

Sean Graham
Sean Graham
2,150 Points

I had to cheat on the Initializing Optional Code Values Code Challenge

I really felt this code challenge went beyond scaffolding upon what I've already learned and entered a realm an unknown realm. I ended up giving up and going to the forums to look for the answer. Even after reviewing the answer, I never would have come up with it. I guess maybe I don't have a good enough grasp on the fundamentals as I thought. I am going to go back in the Track to Object Oriented Swift 3 and review again, but I am really curious if anyone else had as much frustration with this challenge as I did. I've been following pretty much everything up till now and I feel I was thrown off a cliff.

s t
s t
2,699 Points

Hey, I was just in the process of searching for the answer and found your post. I have often had to "cheat" and find the answer, I don't look at it as cheating though, because most times I have tried as best I can before looking, once I find the answer, I dissect it myself and learn how the correct answer was arrived at thus learning what I needed to know. Don't get frustrated, as you keep moving forward, you will come across the same concept again but this time will be armed with the previous knowledge you have gained from "cheating". Goodluck!

I felt the same way

1 Answer

Anthia Tillbury
Anthia Tillbury
3,388 Points

I disagree, although I had to do the same, I don't see anything in the answer that wasn't in the previous video. The problem was the way the question was worded:

"Your task is to create a failable initializer that accepts a dictionary of type [String : String] as input and initializes all the stored properties. (Hint: A failable init method is one that can return nil and is written as init?)."

I got how to make a failable initialiser, that was just: "init?(pubDate: [String : String]) {"

The part that confused me was: "Use the following keys to retrieve values from the dictionary: "title", "author", "price", "pubDate""

I then added those into the initialiser, but didn't know what TH meant when they said "retrieve values from the dictionary", I didn't get that bit at all, and so I tried something crazy like:

"init?(title: [String : String], author: [String : String], price: [String : String]?, pubDate: [String : String]?) {"

I would have worded the question something like the following:

"Check the constants to see if they have stored properties to unwrap them safely (this hints toward a guard statement), the nil value is already mentioned."

I don't know why they went with a struct either, why not a class, I didn't think to have to initialise with self as in a class because structs initialise themselves, even though the task is to initialise there didn't seem to be any errors.

I understand what you mean about price and pubDate being passed into a dictionary with box brackets, it doesn't seem like I would be able to get away with that?

Anyway, I think the text needs to be re-written, I don't think any amount of time would have helped me with this one, it just needed better directions. I do appreciate that too detailed a question gives the answer away, but so what, this is a beginner's course, I'm not here and paying for this to be caught out by "tricky" questions, just make it easy so I can figure it out.