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 trialMichael Gössl
1,275 Pointscould not compile
i think my code is right, but it gives me always an error. could someone help me pleas
struct Tag {
let name: String
}
struct Post{
var title: String
var author: String
var tag: Tag
init(title: String, author: String, tagi: String){
self.title = title
self.author = author
self.tag = Tag(name: tagi)
}
func description() -> String{
return ("\(title) by \(author). Filed under \(tag)")
}
}
let firstPost = Post(title: "iOSDevelopment", author: "Apple", tagi: "swift")
let postDescription = firstPost.description()
2 Answers
Anjali Pasupathy
28,883 PointsThere's really only one problem with your code: in the description function, you need to use tag.name, not tag, in the interpolated string:
return ("\(title) by \(author). Filed under \(tag.name)")
If you make that fix, the code you have written should work fine in Xcode. However, the challenge still won't accept it as a valid answer.
This is because of your Post init method. The quiz compiler expects you to pass in a Tag object when initializing Post, not the name property of a Tag object. It also expects you to use the names "title", "author", and "tag" for the parameter names you're passing into the init method. Because of this, you need to make the following changes to pass:
struct Tag {
let name: String
}
struct Post{
var title: String
var author: String
var tag: Tag
// 1. CHANGE tagi TO tag
// 2. CHANGE tag TYPE TO Tag, NOT String
// 3. CHANGE INITIALIZATION OF self.tag TO REFLECT ABOVE CHANGES
init(title: String, author: String, tagi: String){
self.title = title
self.author = author
self.tag = Tag(name: tagi)
}
func description() -> String{
// CHANGE tag TO tag.name
return ("\(title) by \(author). Filed under \(tag)")
}
}
// 4. CHANGE INSTANTIATION OF firstPost TO REFLECT CHANGES MADE IN INIT METHOD
let firstPost = Post(title: "iOSDevelopment", author: "Apple", tagi: "swift")
let postDescription = firstPost.description()
I hope this helps!
Michael Gössl
1,275 Pointsthanks that work
Anjali Pasupathy
28,883 PointsYou're welcome!