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 PointsWhat am i doing wrong
i cant figure it out
struct Tag {
let name: String
}
struct Post {
var title: String
var author: String
var tag: Tag
}
func description() {
let firstPost = Post(title: "iOS Development", author: "Apple", tag: Tag(name: "Swift"))
return firstPost
}
let postDescription = description()
1 Answer
jcorum
71,830 PointsThe function needs to be inside the struct. Further, you don't hardcode values into the function's return, you interpolate the values of its stored properties into the String. Here are the steps: When you create a Post struct you pass it values. The (unseen) constructor assigns those values to the stored procedures. When you call the func it puts those stored values into the String using String interpolation. Then it returns the String.
struct Post {
let title: String
let author: String
let tag: Tag
func description() -> String {
return "\(title) by \(author). Filed under \(tag.name)"
}
}
let tag = Tag(name: "swift")
let firstPost = Post(title: "iOS Development", author: "Apple", tag: tag)
let postDescription = firstPost.description()
You don't have to create the tag first. You can create it the way you do, but I break it out because I think it is clearer what is going on.