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

william fashanu
william fashanu
2,108 Points

what is wrong wit my code

We learned about instance methods before. For this task, add an instance method to Post.

Name the method description. It takes no parameters and returns a string that describes the post instance. For example given a title: "iOS Development", author: "Apple", and a tag named "swift", the description would read as follows"

"iOSDevelopment by Apple. Filed under swift"

Once you have an instance method, call it on the firstPost instance and assign the result to a constant named postDescription

struct Tag { let name: String }

struct Post { let title: String let author: String let tag: Tag

func description() -> String { return "(title) by (author). Filed under (tag)" } }

let firstPost = Post(title: "lol", author: "loll", tag: Tag(name: "will")) let postDescription = firstPost.description()

2 Answers

Ryan Maneo
Ryan Maneo
4,342 Points

Very close! You forgot to add \ before (title), (author), and (tag) respectively. Also, I personally wouldn't type all of the code on a single line like you did. It's MUCH easier to read separated onto different lines.

Your code:

struct Tag { let name: String }

struct Post { let title: String let author: String let tag: Tag

func description() -> String { return "(title) by (author). Filed under (tag)" } }

let firstPost = Post(title: "lol", author: "loll", tag: Tag(name: "will")) let postDescription = firstPost.description()

My code:

struct Tag {
    let name: String
}

struct Post {
    let title: String
    let author: String
    let tag: Tag

    func description() -> String {

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

    }
}

let firstPost = Post(title: "lol", author: "lol", tag: Tag(name: "Swift"))
let postDescription = firstPost.description()

// You also forgot to use \(tag.name) instead of \(tag), that way you are returning the name rather than the tag constant.
william fashanu
william fashanu
2,108 Points

thank you, it works now. why would it be tag.name and not Tag.name ?

Ryan Maneo
Ryan Maneo
4,342 Points

Because "tag" is an instance of Post. Where as "Tag" is a custom type, and holds no value by itself. Good luck! :)