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

I really need help.

I tried the next code challenge, and this is what I came up with. But, if i try to use this code the whole thing lights up like a Christmas tree. Can anybody please help explain how I messed up this terribly?

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"]
        return? Book(title: title, author: author, price: price, pubDate: pubDate)
        }
         else {
      return nil
    }
}

1 Answer

Oliver Duncan
Oliver Duncan
16,641 Points

There are a few problems with this, let's take a look.

  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"] // Here's where you need to assign variables to your struct, not create new variables 
        return? Book(title: title, author: author, price: price, pubDate: pubDate) // You don't need a question mark after the return statement. Also, initialization methods don't return a value(except for failable inits, which can return nil), they just assign variables.
        }
         else {
      return nil
    }
}

With these issues in mind, let's see the proper way to initialize the Book struct with a failable initializer.

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"] {
      // Assign stored properties here, don't return anything
      self.title = title
      self.author = author
      self.price = dict["price"]
      self.pubDate = dict["pubDate"]
    } else {
      return nil // Initialization fails if title or author is nil
    }
  }

}

Here's another way of writing the same method, this time a guard statement.

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"]
}