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 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.

optionals.swift
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
init?(dict:[String:String]){
        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
seal-mask
.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!