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 trial

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

Jordan Ward
Jordan Ward
2,395 Points

Hey guys, this seems so easy, not sure whats going on, any help would be great!

A little stumped not sure whats wrong..

optionals.swift
struct Book {
    let title: String
    let author: String
    let price: String?
    let pubDate: String?
    }

    init?(dict String: String) {
       title = "Harry Potter"
       author = "J.K.Rowling"
       price = nil
     pubDate = nil

}

1 Answer

Your first issue is your dict parameter. Remember to always place : after the name, also since a dictionary is made up of key value pairs you have to specify the type as such. Since its a failable initializer you also need a guard or an if let statement in there. Check the last video. Next price and pubDate are not nil. They need to be set to the value of the price and pubDate keys.

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"]
    }
}
Jordan Ward
Jordan Ward
2,395 Points

Thanks dude, I didn't know you needed a guard or if let for a failable initialiser, thanks for learning that up

Jordan Ward
Jordan Ward
2,395 Points

Also why do you reference from dict and not book? I don't get that part..

Book is the name of the struct. You would only use Book to access this outside of its scope in another class. dict is a dictionary so it contains key value pairs and you use that to set the constants at the top with the value of the key.

self.title references the test constant at the top. A better way to understand it would be to name the guard let constants something different as below.

struct Book {
    let title: String
    let author: String
    let price: String?
    let pubDate: String?

    init?(dict: [String: String]){

    guard let bookTitle = dict["title"], let bookAuthor = dict["author"] else {
        return nil
    }
    self.title = bookTitle
    self.author = bookAauthor
    self.price = dict["price"]
    self.pubDate = dict["pubDate"]
    }

By saying bookTitle = dict["title"] you are saying make the bookTitle object the value of the key "title" inside dict. If it is "title": "Game of Thrones" than bookTitle would be the string "Game of Thrones".