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 Swift 2.0 Enumerations and Optionals Introduction to Optionals Initializing Optional Values

Alexandre Serra Jaumot
Alexandre Serra Jaumot
6,977 Points

It 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) {
            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
Anjali Pasupathy
28,883 Points

Try 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!