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

William Forbes
William Forbes
21,469 Points

init optional

Can somebody provide me some clarity on on this question and point me in the right direction I have been stuck on it for hours and can't really seem to clarify what the right course of action is nor can I find an answer in the community. I can seem to figure out even how to do it using a force unwrap. Thanks

optionals.swift
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.title = dict["title"]!
    self.author = dict["author"]!
    self.price = dict["price"]
    self.pubDate = dict["pubDate"]

}
Vi Phung
Vi Phung
2,915 Points

There's no need to assign...

self.title = dict["title"] self.author = dict["author"]

You already assigned the values using guard. Simply assign it to itself will do. see below...

self.title = title self.author = author

5 Answers

Vi Phung
Vi Phung
2,915 Points

One of the task of this exercise is to create a fail able initialiser that accepts a dictionary as input, therefore you must pass in a dictionary through the parameter.

When initialising properties you must assign a value/ imaginary value to initialise it. However in this case we are passing in a dictionary and dictionaries comes with a Key and Value pair. In the guard statement, you are simply assigning title with a dictionary key( "title"), not the value. This is possible because dictionaries can be read, update, and remove values by simply using it's key with a method. For example, dict["title"] = "Swift 2.0"

I hope this helps.

Vi

William Forbes
William Forbes
21,469 Points

So since we are assigning the title with just dictionary key( "title"), and not the value. Is there something put in the value by default?

Vi Phung
Vi Phung
2,915 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.title = title
    self.author = author
    self.price = dict["price"]
    self.pubDate = dict["pubDate"]
}

}```

William Forbes
William Forbes
21,469 Points

Thank you for the info!, Anyway you could explain why it was necessary to use a dictionary as the input for the intitializer? Also how in the guard statement I am able to input a single string surrounded by brackets as the value for title and author, since a dictionary is the parameter for the init function should title and author in the guard statement require a value of // title = dict["title": ______]?

Thanks pretty confused on this particular challenge.

Vi Phung
Vi Phung
2,915 Points

So since we are assigning the title with just dictionary key( "title"), and not the value. Is there something put in the value by default?

I'm not sure on this one. if you think of nil as a value then nil must be the default value that can only be use with Optionals.