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 trialLeslie Borrell
2,318 Pointsshowing errors in tree house but not in xcode. no compile errors are showing
struct Post {
let title: String
let author: String
let tag: Tag
init(title: String, author: String, name: String) {
self.title = title
self.author = author
self.tag = Tag.init(name: name)
}
func description() -> String {
return "\(title) by \(author). Filed under \(tag.name)"
}
}
let firstPost = Post.init(title: "Leslie", author: "Borrell", name: "Fiction")
let postDescription = firstPost.description()
struct Tag {
let name: String
}
struct Post {
let title: String
let author: String
let tag: Tag
init(title: String, author: String, name: String) {
self.title = title
self.author = author
self.tag = Tag.init(name: name)
}
func description() -> String {
return "\(title) by \(author). Filed under \(tag.name)"
}
}
let firstPost = Post.init(title: "Leslie", author: "Borrell", name: "Fiction")
let postDescription = firstPost.description()
1 Answer
Greg Kaleka
39,021 PointsHi Leslie,
A couple of things about your code:
- For a Struct, there's no need for an initializer if you're only setting self.property = property. This comes for free with Structs
- You don't need to call the init method directly. Instead of Tag.init(...), you can simply call Tag(...)
- Your third argument when you initialize Post is a String, but should be a Tag.
This should solve the issue:
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: "Leslie", author: "Borrell", tag: Tag(name: "Fiction"))
let postDescription = firstPost.description()
Leslie Borrell
2,318 PointsLeslie Borrell
2,318 PointsHi, Thanks for the response. I understand the the init is not required, but is there a reason that causes the code to fail? I updated the code per your suggestion, but couldn't get it to pass until i deleted the init function.
Thanks, Leslie