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 trialshane reichefeld
Courses Plus Student 2,232 PointsI cant figure out how to solve this
Guided step solution please
struct Tag {
let name: String
}
struct Post {
var title: String
var author: String
var tag: Tag
func description(){
return Post(title: "iOS Development", author: "Apple", tag: Tag(name: "Swift"))
}
}
let firstPost = description()
let postDescription = firstPost
1 Answer
Keli'i Martin
8,227 PointsFor the first task, you've got part of it correct. You've set up your struct correctly. However, the next part of that challenge wants you to create an instance of Post and assign it to the constant firstPost. In the first challenge, you shouldn't have created the description()
method yet.
What you want to do is the following:
let firstPost = Post(title: "Sample Title", author: "Sample Author", tag: Tag(name: "Sample Tag"))
In the second task, you are asked to create the description()
method. This method should contain no parameters but it should return a String. So your function definition would look like this:
func description() -> String
Inside the method, you are asked to return a String which, in the case of the inputs I used above, would look like this:
Sample Title by Sample Author. Filed under Sample Tag
So your description()
method, in full, would look like this:
func description() -> String {
return "\(title) by \(author). Filed under \(tag.name)"
}
Here, you are using string interpolation to create the string with the struct's members.
Last, you are asked to assign the return of description()
to the constant postDescription
. To do that, you simply do:
let postDescription = firstPost.description()