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 trialAlexandre Serra Jaumot
6,977 PointsIt works in Xcode but not in the Editor..
import Foundation
struct Book {
let title: String
let author: String
let price: String?
let pubDate: String?
init?(dict: NSDictionary) {
guard
let titleName = dict["title"] as? String,
let authorName = dict["author"] as? String
else {return nil}
self.title = titleName
self.author = authorName
self.price = dict["price"] as? String
self.pubDate = dict["pubDate"] as? String
}
}
1 Answer
Anjali Pasupathy
28,883 PointsTry using the type [String: String] rather than NSDictionary for the parameter in your init method. This eliminates the need to import Foundation and to optionally unwrap the values as Strings. It also allows you to make sure the argument the user passes into Book upon instantiation has the correct types for both the keys and the values, which decreases the probability of a runtime error or a bug.
I hope this helps!