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 trialGolf Ataboonwongse
4,061 PointsWhat's wrong with my code?
The questions of this code challenge is as follows:
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
I have tried coding it directly on Xcode already and it works. However, it still reply me with an error. Could anyone help me figuring it out? Thank you
struct Tag {
let name: String
}
struct Post {
let title: String
let author: String
let tag: Tag
init(title: String, author: String, tagName: String) {
self.title = title
self.author = author
self.tag = Tag(name: tagName)
}
func description() -> String {
return "\(title) by \(author). Files under \(tag.name)"
}
}
let firstPost = Post(title: "iOS Development", author: "Apple", tagName: "Swift")
let postDescription = firstPost.description()
2 Answers
Richard Hanolt
3,726 PointsThe following worked for me but it's highly unlikely that I did it the best way:
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: "My First iOS App", author: "Rick Hanolt", tag: Tag(name: "Swift"))
let postDescription = firstPost.description()
Bill Merickel
11,420 PointsYour string says Files, not Filed. Try that.
Golf Ataboonwongse
4,061 PointsThank you very much Bill but it still doesn't work. To pass this code challenge, I need to do as Richard said. However, both approaches give the same result.
Golf Ataboonwongse
4,061 PointsGolf Ataboonwongse
4,061 PointsThank you very much for your advice, Richard. I could pass this code challenge using your approach. Both approaches give the same result (my version with typo correction of "Filed"), however, I do agree with you that this is not the best way.
Again, thank you very much for your help. :)