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

Randell Pur
Randell Pur
4,934 Points

I am not sure why this wont pass

It doesn't show any errors in Xcode, and it says to check the "Preview " but it doesn't show anything in there.

The error I get says:

Bummer: Your code could not be compiled. Please click on "Preview" to view the compiler errors.

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


func book (bookDictionary: [String : String]) -> Book? {
    guard let title = bookDictionary["title"], let author = bookDictionary["author"]  else {
        return nil
    let price = bookDictionary["price"]
    let pubDate = bookDictionary["pubDate"]

    return Book(title: title, author: author, price: price, pubDate: pubDate)

3 Answers

Gavin Brown
Gavin Brown
4,560 Points

create an init? method within the struct like this init?(dict[String:String]){ //initialize values here}

Gavin Brown
Gavin Brown
4,560 Points
        if let title = dict["title"], let author = dict["author"]{
            let price = dict["price"]
            let pubDate = dict["pubDate"]
            self.price = price
            self.title = title
            self.author = author
            self.pubDate = pubDate
        } else {
            return nil
Wouter Willebrands
.a{fill-rule:evenodd;}techdegree seal-36
Wouter Willebrands
iOS Development with Swift Techdegree Graduate 13,121 Points

Hi Randell,

You are actually very close, but are missing some pieces.

The assignment requests you to create a failable initializer that accepts a dictionary of type [String : String] as input and initializes all the stored properties. Remember that failable inits methods can return nil and is written as init as Gavin stated.

You correctly wrote your guard statements although in the not within the assignment they want you to use "dict" instead of "bookDictionary".

If you change that part to:

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

that part should work. Next is initializing all of the properties inside you struct Book. You can do that by adding:

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

within your method.

Please let me know if you have any more questions. Good luck!