Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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

John Wilcox
John Wilcox
4,233 Points

Extra argument in call?

Hi, I'm getting an 'extra argument author in call' error. I can't figure out why - author is unwrapped in the guard statement. The memberwise init does call for author. Any suggestions?

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
      } 

      let price = dict["price"]
      let pubDate = dict["pubDate"]

      Book(title: title, author: author, price: price, pubDate: pubDate)
    }

}

1 Answer

David Papandrew
David Papandrew
8,386 Points

Hi John,

You want to bind the values passed in as arguments to the init method to the Book properties. There's also an extraneous line of code in the init method with a class declaration and parameters. You should remove that.

Here's the corrected code:

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"]
    }
}
John Wilcox
John Wilcox
4,233 Points

Bah, you're right. I see that I should have just assigned to the parameters directly. Thanks - sometimes I truly think I'm dyslexic when it comes to code.

What do you mean when you said "There's also an extraneous line of code in the init method with a class declaration and parameters." I see I can omit the underscore before the word dict. Otherwise however, to my eyes our guard statements look alike.

David Papandrew
David Papandrew
8,386 Points

Hi John,

The line I was referring to is this one in the init method:

Book(title: title, author: author, price: price, pubDate: pubDate)

But maybe that was how you were trying to assign the parameters. Either way, good luck!