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

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