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

David Carr
David Carr
1,070 Points

Code not accepted, but there are no errors. What am I doing incorrectly?

I've ran this in swift playground and initialized a constant with and without titles and authors in the dictionary. It seems to work just fine. What am I doing wrong, that prevents it from being accepted?

Thanks!

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

    init?(book: [String : String]) {
      guard let title = book["title"], let author = book["author"] else {
        return nil
      }
      let price = book["price"]
      let pubDate = book["pubDate"]

      self.title = title
      self.author = author
      self.price = price
      self.pubDate = pubDate
    }
}

1 Answer

Dan Lindsay
Dan Lindsay
39,611 Points

Hey David,

It looks like you used "book" instead of "dict"(which the challenge asks for you to use as the name) as your argument name. All your code is great, just need to switch "book" with "dict" in all places in the init method and you will pass the challenge.

Dan