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 trialJAMES COGSWELL
1,524 PointsMy code is correct but treehouse compiler gets errors.
Alright I found the solution to my previous question. I just needed to request .name from tag i.e. "\(tag.name)" in the interpolated string. I've updated the question since now I have a new problem:
Now the treehouse editor says I have compiler errors, but there are no errors on the Preview output, and the code works fine in my xcode playground.
If you check the output of the print statement, you can see that it's printing the entire struct contents, i.e. Tag(name: String) instead of just "String". I think that is why the code challenge keeps failing. It's looking for a "String" obtained by interpolation, but my code isn't the way it should be. Why does the string interpolation print Tag(name: String) instead of "string"?
For reference, the question wants this line returned -> "\(title) by \(author). Filed under \(tag)"
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 {
print ("\(title) by \(author). Filed under \(tag.name).")
return ("\(title) by \(author). Filed under \(tag.name).") // tag.name added after original question posted.
}
}
let firstPost = Post(title: "DogApp", author: "Dogman", tag: "test")
let postDescription = firstPost.description()
3 Answers
Richard Lu
20,185 PointsHi James,
Currently, I see that you're creating the tag during the initialization of the Post, but there is an easier way that you can accomplish this task. When you create a struct, you are automatically given an initializer without needing to writing. For example, your Post struct would automatically have the following initializer
// You can write this, but it's not necessary
init(title: String, author: String, tag: Tag) {
self.title = title
self.author = author
self.tag = tag
}
Another thing I'd like to point out is the format of the answer. The challenge wants you to format the description as follows:
Note - there is no period at the end of this sentence
"\(title) by \(author). Filed under \(tag)"
Lastly, while accessing tag in your description method, you're accessing it as it's instance property. What you ultimately want is tag.name
.
Let me know if there are any additional questions.
Happy Coding,
- Rich
JAMES COGSWELL
1,524 PointsAlright so if you change the interpolations in the return statement from \(tag) to \(tag.name) the return statement works fine in Xcode. But now I'm getting compiler errors in the treehouse editor. :'(
JAMES COGSWELL
1,524 PointsHey Rich
Thanks for the help. That was the solution.