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 trialimelda cloutier
Courses Plus Student 2,597 Pointswhat is the request, a func? can't name a dictionary....
when in Xcode I try to give a name to an init method it tells me I can't name it
struct Book {
let title: String
let author: String
let price: String?
let pubDate: String?
init? (title: String, author: String, price: String?, pubDate: String?) {
self.title = title
self.author = author
self.price = price
self.pubDate = pubDate
}
}
4 Answers
Matthew Long
28,407 PointsThe wording of the challenge might be throwing you off. You're not supposed to give the init
itself a name. You're supposed to give the init
method an accepted argument named dict
that's a dictionary type of [String: String]
.
Next you're supposed to determine if title
and author
exist, because those are necessary. I did it with a guard
statement. If they don't exist then you're asked to return nil
.
After the guard statement you initialize the stored properties. The title
and author
are self explanatory, like you have. However, the price
and pubDate
are the values from the dictionary dict
if they exist.
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"]
}
}
You will need to unwrap the price
and pubDate
values.
Hopefully that clears things up some. This is still not easy for me either, but I tried explaining it best I know. Happy coding!
imelda cloutier
Courses Plus Student 2,597 PointsI still don't get why your code pass. anybody would have a var that use the code to help me understand?
Matthew Long
28,407 PointsWhere are you still confused? The part that you were concerned about in your question is the naming of an initializer method or the naming of a dictionary? You don't name an initializer in the way that you could name a function. The challenge asked that you give an argument named dict
of type [String: String]
to the initializer method.
Sorry for any confusion I have caused you.
imelda cloutier
Courses Plus Student 2,597 Pointsno you brought clarification. I am just trying to figure out with the line of code you give, how to use it to create a constant or a variable.
Matthew Long
28,407 PointsOh are you asking how to create an instance of Book
? I can see where this could be confusing especially since I don't think Xcode will autocomplete the whole thing for you in this case. But, it would be a dictionary called dict
with the keys being "title"
, "author"
, and optionally "price"
and "pubDate"
.
let gameOfThrones = Book(dict: ["title": "Game of Thrones", "author": "GRRM", "price": "15", "pubDate": "1996"])
Keep in mind that gameOfThrones
is of type Book?
and not Book
so you would have to unwrap it. If I remember correctly you're currently in the courses teaching you how to unwrap optionals.
imelda cloutier
Courses Plus Student 2,597 PointsI get it now, with the example, the no autocomplete note. thank you.
Zach Hudson
7,702 PointsZach Hudson
7,702 PointsGood explanation!