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 trialJacob Bergdahl
29,119 PointsCompiler 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
Treehouse TeacherThanks 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
29,119 PointsOkay, 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
9,320 PointsHi 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
29,119 PointsI 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
9,320 PointsHmm, 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
29,119 PointsOh 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
9,320 PointsYay, it worked! Thank you so much!
Jens Hagfeldt
16,548 PointsThank you for your post on this Jacob! I have noticed quite often though that the naming of things in these challenges are extremely important.
Jacob Bergdahl
29,119 PointsJacob Bergdahl
29,119 Pointstagging Pasan Premaratne