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 trialMathias Jeppesen
Courses Plus Student 3,199 PointsWon't compile and 'Preview' is empty. Running the code in playground gives the correct output.
Not sure if I'm doing something wrong here or a compiler/interface bug making me unable to see the 'Preview' tab.
struct Tag {
let name: String
}
struct Post {
let title: String
let author: String
let tag: Tag
init(title: String, author: String, tag: String) {
self.title = title
self.author = author
self.tag = Tag(name: tag)
}
func description() -> String {
return "\(title) by \(author). Filed under \(tag.name)"
}
}
let firstPost = Post(title: "iOS Development", author: "Apple", tag: "Swift")
let postDescription = firstPost.description()
2 Answers
Pasan Premaratne
Treehouse TeacherYour code is correct, but unfortunately as Matt Skelton mentions, the challenge is looking for something very specific. In this case the challenge expects the default initializer to be used, which accepts an instance of Tag as an argument and not a String value (for Tag).
Since you've written a custom initializer, there's no way for the compiler to create an instance of your struct which is why it isn't compiling. Think of it this way, while it seems like a logical custom init method, another student could write an initializer that looks like this
init() {
self.title = "iOS Development"
self.author = "Apple"
self.tag = Tag(name: "Swift")
}
This is also a valid init method that would end up with the same result the code challenge is looking for. But again, because I have no way to foresee all the possible variations of the init method, I can only guarantee the standard one will be used.
Hope that makes sense.
Matt Skelton
4,548 PointsHey Mathias,
This one looks a little strange, I don't see any reason offhand why it wouldn't work. All I can assume is that although the challenge doesn't seem to directly say so, it wants you to perform the task in a very specific way.
If you were to remove the initialize method and pass an instance of Tag as a parameter in your method call rather than a String, you'd pass the challenge.
Normally the tasks are very explicit when describing what they require, so I think you've just found a way to complete the task that hasn't been considered.
Good work outsmarting the compiler, keep it up!