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

Christopher Davis
Christopher Davis
8,555 Points

This exact code has passed for other students...

I'm confused here.

I thought that I may have been typing the wrong code, so I checked the help section to see what I was missing. This is the exact code that has passed for moderators and other students from this section...but it doesn't pass for me.

Am I doing something wrong?

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

   init?(dict: [String: String]) {
      guard if let title = dict["title"], let author = dict["author"] {
        return nil
      }

    self.title = title
    self.author = author
    self.price = dict["price"]
    self.pubDate = dict["pubDate"]
    }
}
Rogier Nitschelm
seal-mask
.a{fill-rule:evenodd;}techdegree
Rogier Nitschelm
iOS Development Techdegree Student 5,461 Points

Hello there,

You can bind an optional using, for example, guard let or if let. It seems you tried to combine them both from the looks of it (or it could be a typo :p)

Try one of these:

Guard:

struct Book {
   ...
    init?(dict: [String: String]) {
        guard let title = dict["title"], let author = dict["author"] else {
            return nil
        }
     ....
    }
}

If let:

struct Book {
   ...
    init?(dict: [String: String]) {
        if let title = dict["title"], let author = dict["author"]  {
            self.etc = etc 
             ...
        } else {
         return nil
       }
     ....
    }
}

I personally prefer the guard statement (first one).

2 Answers

Christopher Davis
Christopher Davis
8,555 Points

Thanks so much for spending time with me working through this. I'll give some more info...

Here's the task :

In the editor, you have a struct named Book which has few stored properties, two of which are optional.

Your task is to create a failable initializer that accepts a dictionary of type [String : String] as input and initializes all the stored properties. (Hint: A failable init method is one that can return nil and is written as init?).

Use the following keys to retrieve values from the dictionary: "title", "author", "price", "pubDate"\

Note: Give your initializer argument the name dict


Here's the code that I'm writing for it :

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

Here's the error that I'm getting :

Make sure you are creating a failable initializer that accepts a dictionary as an argument for initialization

Christopher Davis
Christopher Davis
8,555 Points

It looks like my code is now working...I guess there might have been a bug? Thanks for the help though, Rogier!

Christopher Davis
Christopher Davis
8,555 Points

Hey, Roger! Thanks so much for the help. I did have a typo in there that was a remnant of trying to figure out why it wouldn't compile. This is exactly what I'm trying to enter now, which still doesn't work :

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"]
    }
}
Rogier Nitschelm
seal-mask
.a{fill-rule:evenodd;}techdegree
Rogier Nitschelm
iOS Development Techdegree Student 5,461 Points

I don't know the assignment - so I can't really say what needs to be done. What does this struct need to do - that it does not do now? What feedback are you receiving on this when you submit it?

:)