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 Swift 2.0 Enumerations and Optionals Introduction to Optionals Initializing Optional Values

Alexandre Chris
Alexandre Chris
4,042 Points

Code Challenge init?

I dont understand what we are doing in this code challenge and why do we need to initialize in this struct?

3 Answers

Corey F
PLUS
Corey F
Courses Plus Student 6,450 Points

Ok the code challenge wants us to set up an initializer that accepts dictionary keys.

Remember dictionaries by definition are optionals. We ask for a particular value using a key, but there's a chance this key might not exist and be nil.

It doesn't matter if price or pubDate are nil, since they are optional strings and can take nil values. Title and author aren't optionals , they cannot accept nil and require you need to unwrap the optionals before you can use them.

I'd rather not just give you the answer. So I'm going to give you the way you should never solve this, using force unwrapping (!) . Remember this leads to problems. If it turns out a value is nil... we're screwed.

struct Book {
    let title: String
    let author: String
    let price: String?
    let pubDate: String?

    init?(dict: [String : String]) {

        self.title = dict["title"]!
        self.author = dict["author"]!
        self.price = dict["price"]
        self.pubDate = dict["pubDate"]
    }
}

DO NOT DO THIS!

The real answer looks kinda similar. But you can't use (!). So

=  dict["title"]!
= dict["author"]!

will not be part of the actual solution. The correct answer requires us to first use another better method we learned to unwrap.

Alexandre Chris
Alexandre Chris
4,042 Points

Thank you for your answer :)) it makes it all more clear now.

Corey F
PLUS
Corey F
Courses Plus Student 6,450 Points

Your just about there

No need to use force unwrapping (!) .Almost never use it. price and pubDate can accept optionals.

And your missing initializing 2 properties So

self.title = ___
self.author = ____
self.price = dict["price"]
self.pubDate = dict["pubDate"]

So fill in the blanks with the stuff you created for use in your guard statement.

David Sparks
PLUS
David Sparks
Courses Plus Student 8,657 Points

struct Book { let title: String let author: String let price: String? let pubDate: String?

init?(dict: [String : String])  {
    guard let title = dict["title"], author = dict["author"] else {
        return nil
    }
    self.price = dict["price"]!
    self.pubDate = dict["pubDate"]!



}

}

Is closer but can't get this to work either.