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