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 trialElijah Florence
Courses Plus Student 3,225 PointsI am lost
Am I even close?
struct Book {
let title: String
let author: String
let price: String?
let pubDate: String?
init?(dict: [String : String])
{
guard let self.title = dict["title"], let self.author = dict["author"], let self.price = dict["price"], let self.pubDate = dict["pubDate"]
else { return nil }
}
}
1 Answer
Jesse Anderson
iOS Development Techdegree Student 9,702 PointsYou are very close! The guard is to check that the constants that are not options actually exist. If they don't, your returning nil. With that in mind, your also doing an init statement, so you need to finish assigning these values after you verify the constants with the guard statement. I don't think you need the price in the guard as that is an optional. And then don't forget your actual assignments.
struct Book {
let title: String
let author: String
let price: String?
let pubDate: String?
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"]
}
}
Elijah Florence
Courses Plus Student 3,225 PointsElijah Florence
Courses Plus Student 3,225 PointsThanks man That makes a lot of sense