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 Build a Simple iPhone App with Swift Getting Started with iOS Development Swift Recap Part 1

Sam Belcher
Sam Belcher
3,719 Points

Swift Recap Part 1

what am I doing wrong?

structs.swift
struct Tag {
  let name: String
}
struct Post {
  let title: String
  let author: String
  let tag: Tag
  func description() -> String {
    let describe = "\(title) by \(author). Filed under \(tag)"
    return describe
  }
}

let firstPost = Post(title: "Harry Potter", author: "J.K. Rowling", tag: Tag(name: "sam"))
let postDescription = Post.description(firstPost)

2 Answers

Jeff McDivitt
Jeff McDivitt
23,970 Points

Hi Sam -

There are a few things incorrect with your code

struct Tag {
    let name: String
}

struct Post {

    var title: String
    var author: String
    var tag: Tag

    func description() -> String {

//You do not need to have a constant here, just a return also you need .name added to tag to adhere to the struct

        return "\(title) by \(author). Filed under \(tag.name)"

    }

}

let firstPost = Post(title: "The Hero", author: "Jeff McDivitt", tag: Tag(name: "One"))

//You have things a little backwards here, just break it down like calling a normal function and I believe you will see the issue
let postDescription = firstPost.description()

Hope this helps!! Happy Coding :)

Ali C
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Ali C
iOS Development Techdegree Graduate 27,901 Points

I think Jeff explained it nicely, I just don't agree creating the constant for the return object is incorrect. The main issue is that description() does not take any argument, and also it is not a static function, so it can't be called on the Type itself. but you are calling it with argument on the type: Post.description(firstPost).