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

Am I missing something or does the simulator have a bug?

init?(fromDictionary dictionary: Dictionary<String,String>) {
        guard let initTitle = dictionary["title"], let initAuthor = dictionary["author"] else {
            return nil
        }
        title = initTitle
        author = initAuthor

        if let initPrice = dictionary["price"], let initPubDate = dictionary["pubDate"] {
            price = initPrice
            pubDate = initPubDate
        }
    }

2 Answers

The guard statements have to be written separately for the challenge to accept it as a valid answer.

Did you try something like:

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

    init?(dict: [String : String]) {
      if dict["title"] == nil {return nil}
      if dict["author"] == nil {return nil}
      self.title = dict["title"]!
      self.author = dict["author"]!
      self.price = dict["price"]
      self.pubDate = dict["pubDate"]
      }    
}

..per this thread:

https://teamtreehouse.com/community/tried-the-code-in-a-playground-of-xcode-7-its-ok-dont-see-any-errors-in-a-preview-window-here