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

Eric Lakatos
Eric Lakatos
2,338 Points

failable initializers and passing dictionaries. Code challenge

The instruction is:

In the editor, you have a struct named Book which has few stored properties, two of which are optional.

Your task is to create a failable initializer that accepts a dictionary as input and initializes all the stored properties. (Hint: A failable init method is one that can return nil and is written as init?). Name the initializer parameter dict.

Use the following keys to retrieve values from the dictionary: "title", "author", "price", "pubDate"

Im having trouble understanding why and how you would pass a dictionary through the init while also trying to define the initial self values. I could be misunderstanding the wordage completely or could be just off the mark. THe furthest ive gotten to workable code is:

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

    init?(title: String, author: String, price: String?, pubDate: String?){
        guard let price = price, let pubDate = pubDate else {
            return nil
        self.title = title;
        self.author = author;
        self.price = price;
        self.pubDate = pubDate;


1 Answer