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

Endi Hıdır
Endi Hıdır
1,762 Points

I can't understand this question. Can someone help me please ?

QUESTION: 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 of type [String : String] as input and initializes all the stored properties. (Hint: A failable init method is one that can return nil and is written as init?).

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

Note: Give your initializer argument the name dict

optionals.swift
struct Book {
    let title: String
    let author: String
    let price: String?
    let pubDate: String?

     init?(dict: [String: String]) {
        if let title = dict["title"], let author = dict["author"], let price = dict["price"], let pubDate = dict["pubDate"]
        {
            Book(dict: [title: title])
            Book(dict: [author: author])
            Book(dict: [price: price])
            Book(dict: [pubDate: pubDate])
        }

        return nil
        }   
}

You need to focus on your properties, you will notice that the title and author are not optional so you need to check if the value of these properties are nil on your failable initializer. If the two properties that is not optional are nil, so you need to return a nil value. But if price and pubDate are nil, no worries! you should not return a nil.

In a nutshell, you need to check the title and author if nil, if these are nil the initialization will failed. However if the title and author have values and the rest are nil you should still continue the initialization of the struct.

Endi Hıdır
Endi Hıdır
1,762 Points

Amazon Web

I follow your advices and did something. However I still take a "Bummer : Try again" error Please if you know this question's answer, can you write the right answer ? My Codes:

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

init?(dict: [String: String]) {

    let title = dict["title"]
    self.title = title!
    if title == nil {
        return nil
    }
    else
    {
        Book(dict: [self.title: title!])
    }

    let author = dict["author"]
    self.author = author!
    if author == nil {
        return nil
    }
    else{
        Book(dict: [self.author: author!])
    }


    if let price = dict["price"], let pubDate = dict["pubDate"]
    {

        self.price = price
        self.pubDate = pubDate

        Book(dict: [self.price!: price])
        Book(dict: [self.pubDate!: pubDate])
    }
    else
    {
    return nil
    }
}

}

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

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

You will notice that on the first statement in the failable init method, I check if the two non optional properties are not nil (if nil return a nil), then I assign to the properties the unwrapped value I get from the guard statement. I didn't check the value of price and pubDate because these are optionals (so they can be nil),

Endi Hıdır
Endi Hıdır
1,762 Points

Thank you so much Amazon Web