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

Gustavo Creative
Gustavo Creative
2,070 Points

Optional Final Quiz Question - What am I doing wrong??

What is wrong with my code?

init?(dict: [String : String], author: String, price: String?, pubDate: String?, title: String) { self.title = title self.author = author self.price = price self.pubDate = pubDate }

optionals.swift
struct Book {
    let title: String
    let author: String
    let price: String?
    let pubDate: String?
    init?(dict: [String : String], author: String, price: String?, pubDate: String?, title: String) {
      self.title = title
      self.author = author
      self.price = price
      self.pubDate = pubDate
    }
}

1 Answer

Jorge Solana
Jorge Solana
6,064 Points

Hey Gustavo!

Looking at the code challenge, it says you get a dictionary named dict as input for your init method. In your code you are also writing the dictionary parameters as input. Just for an explanation, this dictionary might be something like this:

let dictionary = ["title" : "someTitle", "author" : "someAuthor", 
"price" : "anyPrice", "pubDate" : "someDate"]

This way, when you write

init?(dict: [String : String])

you already got those title, author, price and pubDate in the dictionary dict

The challenge wants you to practice the guard let statement for those declared properties (title and author) that are not optionals.

Keep up the hard work!