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 trialAananya Vyas
20,157 Pointscant understand what to do
initialised all prop of struct ... put the guard stmt... what else?!
struct Book {
let title: String
let author: String
let price: String?
let pubDate: String?
init?(dict: [String: String]) {
guard let price = dict["price"], let pubDate = dict["pubDate"] else {
return nil
}
self.title = title
self. author = author
self.price = dict["price"]
self.pubDate = dict["pubDate"]
}
}
3 Answers
Jorge Solana
6,064 PointsYou have a blank space between self. and author. Author is a property of self, so it needs to be typed together or compiler will not understand.
self.author = author
I already suggest you try to keep your code clean. I mean, try to put your parenthesis and other marks ("[", "{", "(", etc.) in position so you can easily get where some condition, class or whatever ends and finishes.
Example:
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"]
}
}
Hope it helps!
Jorge Solana
6,064 PointsHi Aananya!
Since it's a title bit confusing, I'm going to c&p some documentation text:
It is sometimes useful to define a class, structure, or enumeration for which initialization can fail. This failure might be triggered by invalid initialization parameter values, the absence of a required external resource, or some other condition that prevents initialization from succeeding.
A failable initializer creates an optional value of the type it initializes. You write return nil within a failable initializer to indicate a point at which initialization failure can be triggered.
What I understand it does is that the initializer spect the properties as optionals, like everything could go wrong. So since price and pubDate are already optionals, we need to add some control to the author and title properties. We do so using the guard let statement, so if anything goes wrong there, we break the current scope of the init method (by returning nil). So final code will be like this:
guard let title = dict["title"], let author = dict["author"] else {
return nil
}
Hope it helps and happy coding!
Aananya Vyas
20,157 Points 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"]
}
}```
Aananya Vyas
20,157 Pointsi understood but..still isn't working...