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 trialAdam Petritsis
2,942 PointsHow can we avoid the "Extra argument ' ' in call" error?
In the editor, you have a struct named Book which has few stored properties, two of which are optional.
Your task is to create a failable initializer that accepts a dictionary of type [String : String] as input and initializes all the stored properties. (Hint: A failable init method is one that can return nil and is written as init?).
Use the following keys to retrieve values from the dictionary: "title", "author", "price", "pubDate"
Note: Give your initializer argument the name dict
struct Book {
let title: String
let author: String
let price: String?
let pubDate: String?
init?(dict: [String: String]) {
return nil
}
}
func result(dict: [String : String]) -> Book? {
guard let title = dict["title"], let author = dict["author"] else {
return nil
}
let price = dict["price"]
let pubDate = dict["pubDate"]
return Book(title : title, author: author, price: price, pubDate: pubDate)
}
1 Answer
Dan Lindsay
39,611 PointsHey Adam,
It looks like your init method is incomplete. You didn’t need to create a result function outside your struct either, but some of the code you have in that function should be in your init method.
First, lets take that guard statement(which is the correct logic, just in the wrong place) out of the result function, and put it in our init, like so:
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
}
}
}
Now we just need to reference our structs internal properties and assign them appropriately, like this:
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 refers to the original constant(let title: String), and title refers to the constant created in the guard statement
self.title = title
self.author = author
self.price = dict[“price”]
self.pubDate = dict[“pubDate”]
}
}
and that will pass. You already took care if assigning the title and author to the appropriate dictionary strings in the guard statement, you just needed to take care of the price and pubDate.
Hope this helps
Dan
Adam Petritsis
2,942 PointsAdam Petritsis
2,942 PointsSuper, that makes sense! Thanks Dan!