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!

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 trial

iOS Enumerations and Optionals in Swift Introduction to Optionals Initializing Optional Values

Andrew Lundy
Andrew Lundy
13,269 Points

When I use 'Book' it's returning nil and I believe it should be returning something else

Okay, maybe I'm not understanding this completely. But when I use the Book struct in a constant, it is coming out as nil. Below is my code, and from what I understand, it should return 'Game of Thrones' as the title, and 'George R. R. Martin' as the Author. I'm obviously doing this wrong, since the I'm getting an error saying there's too many arguments. I understand there is too many arguments, but what I don't understand is where then, is the dictionary supposed to be created and used? I've been rocking it up until this point in the course, and I don't want to continue until I have a full understanding of what I'm doing wrong.

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"]

let newBook = Book(dict: ["title" : "Game of Thrones"], ["author" : "George R. R. Martin"]);

1 Answer

Andrew Lundy
Andrew Lundy
13,269 Points

I figured it out. Such a small mistake. Thanks guys.