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

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!