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

Ingunn Augdal Fløvig
Ingunn Augdal Fløvig
1,507 Points

Failable initializer and dictionary confusion

Hello everyone,

I couldn't complete this task but pieced this together from answers I found and am stuck with a piece of code I don't quite understand how executes the task. It works, I am just wondering how.

The initializer accepts an argument in the form of a dictionary, but where does the dictionary come from? The constants for book, author, price and pubDate do not form a dictionary, right? So are we creating a dictionary when we write the initializer? I am just really confused and in need of some explanation.

Thanks,

  • Ingunn
optionals.swift
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"]


    }
}

1 Answer

Jhoan Arango
Jhoan Arango
14,575 Points

Hello,

So in this code you have there, the initializer takes in a dictionary. Here you do not see the dictionary because there is none, but we can create one and pass it to the initializer.

Basically you are creating an object that takes a dictionary and parses the dictionary to initialize it's properties, or in other words to give values to the properties of the struct.

struct Book {

// Properties to initialize 
    let title: String
    let author: String
    let price: String?
    let pubDate: String?

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

// Making sure that the dictionary has these values
    guard let title = dict["title"], let author = dict["author"] else {
    return nil
    }

// Initializing the properties from the dictionary 
    self.title = title
    self.author = author
    self.price = dict["price"]
    self.pubDate = dict["pubDate"]

    }
}

// Dictionary
let bookDictionary = [
    "title":"Swift 4.0",
    "author": "Apple",
    "price": "19,99",
    "pubDate" : "03/26/2018"
]

// creating an instance of Book by passing it a dictionary
let book = Book(dict: bookDictionary)

book?.title // Prints "Swift 4.0"

Hope this helps you.. If not let me know I can explain further.