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

Jacob Bergdahl
Jacob Bergdahl
29,119 Points

Compiler error during code challenge

I keep getting an error message saying my code couldn't be compiled when I try to check my work, however there are no compiler errors, and it compiles fine when I try it in other compilers. Can someone help me identify what the problem could be? Thanks!

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

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

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

EDIT: To anyone else reading this and wanting to know what the problem is, I found out that it's the name of the variable. It must be named dict -- not dictionary or anything else. This solution will work if you rename the variable.

Pasan Premaratne
Pasan Premaratne
Treehouse Teacher

Thanks for the tag Jacob Bergdahl. This is a limitation on our end. I've updated the directions to state that you need to name the parameter dict for the meantime. I'll get a different version up soon that doesn't require this.

2 Answers

Jacob Bergdahl
Jacob Bergdahl
29,119 Points

Okay, I finally finished it. Apparently there's a bug in the compiler that prevents it from accepting guard as valid input, even though the videos before it clearly show that it's preferable to use guard. I'm marking this as answered, but I'd still love to hear from Pasan Premaratne if there's anything wrong in my code that I should be made aware of!

Marina Alenskaja
Marina Alenskaja
9,320 Points

Hi Jacob

Can you please tell me how you finished it without using guard? Did you use if let or do something else to fix the issue? I tried this, but it didn't work and again - no compiler errors..

init?(dictionary: [String: String])  {
       if let title = dictionary["title"], author = dictionary["author"] { 
        let price = dictionary["price"]
        let pubDate = dictionary["pubDate"]

        self.title = title
        self.author = author
        self.price = price
        self.pubDate = pubDate
        } else {
          return nil 
        }
}
Jacob Bergdahl
Jacob Bergdahl
29,119 Points

I used this quick-fix solution:

init?(dictionary: [String : String]) {
      if dictionary["title"] == nil {
          return nil
      }
      if dictionary["author"] == nil {
          return nil
      }

      self.title = dictionary["title"]!
      self.author = dictionary["author"]!
      self.price = dictionary["price"]
      self.pubDate = dictionary["pubDate"]
}

The code doesn't look as nice as when using guard, but this solution is at least accepted without compiler errors.

Marina Alenskaja
Marina Alenskaja
9,320 Points

Hmm, it still doesn't work, even though I copy pasted your code... :

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

    init?(dictionary: [String : String]) {
      if dictionary["title"] == nil {
          return nil
      }
      if dictionary["author"] == nil {
          return nil
      }

      self.title = dictionary["title"]!
      self.author = dictionary["author"]!
      self.price = dictionary["price"]
      self.pubDate = dictionary["pubDate"]
      }
}
Jacob Bergdahl
Jacob Bergdahl
29,119 Points

Oh wow, I just realized what the problem is. The dictionary MUST be named dict. Change the variable name dictionary to dict and any of the solutions posted here will work. Very bizarre error. Tagging Rob Allessi, maybe you can let the team and/or Pasan know? Thanks.

Marina Alenskaja
Marina Alenskaja
9,320 Points

Yay, it worked! Thank you so much!

Jens Hagfeldt
Jens Hagfeldt
16,548 Points

Thank you for your post on this Jacob! I have noticed quite often though that the naming of things in these challenges are extremely important.