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 trialRichard Sun
11,257 PointsWhy does it say to use String Interpolation if I am already using it?
I've tried different things, like trying to return the String without storing it first. I don't know what I'm doing wrong.
struct Tag {
let name: String
}
struct Post {
var title: String
let author: String
var tag: Tag
init (title: String, author: String, tag: Tag) {
self.title = title
self.author = author
self.tag = tag
}
func description () -> String {
let description = "\(title) by \(author). Filed under \(tag)"
return description
}
}
let firstPost = Post(title: "iOS Development", author: "Apple", tag: Tag(name: "swift"))
let postDescription: String = firstPost.description()
1 Answer
Jason Anders
Treehouse Moderator 145,860 PointsHey Richard,
It's sort of a misleading error, as the main problem lies inside the instance method and not the actual interpolation of the string (though there is an error in there too).
- The method itself is called
description
. The challenge did not ask you to assign the interpolated string to a constant called the same. You just need to return the string, so you're 2 lines should only be one. - The compiler won't let you just interpolate
tag
without the member. So... it needsname
which is the member you want in the string.
I have provided the corrected method for you to review. Have a look and what was changed and also why.
func description () -> String {
return "\(title) by \(author). Filed under \(tag.name)"
}
Keep Coding! :)
Richard Sun
11,257 PointsRichard Sun
11,257 PointsThanks a lot!