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 trialRoyce Pollard
24,267 PointsI am getting an error about following quiz directions using string interpolation. However, I think I'm using correctly.
Not sure if it is bug in the quiz but from my code I do not understand what exactly the quiz wants. This is what it entails.
///////////
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"
"iOS Development 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 even changed the values of firstPost to imitate the those of the example and I still do not pass. Any suggestions?
struct Tag {
let name: String
}
struct Post {
var title: String
var author: String
var tag: Tag
func description() -> String {
return "\(title) by \(author). Filed under \(tag)"
}
}
let firstPost = Post(title: "iOS Development", author: "Apple", tag: Tag(name: "swift"))
let postDescription = firstPost.description()
2 Answers
Steven Deutsch
21,046 PointsHey Royce Pollard,
You're just interpolating the wrong thing inside your description method. You need to be accessing the name property of the tag instance. You can do this using dot syntax.
struct Tag {
let name: String
}
struct Post {
var title: String
var author: String
var tag: Tag
func description() -> String {
// We want to access the Tag instance's name property
// We do this by tag.name
return "\(title) by \(author). Filed under \(tag.name)"
}
}
let firstPost = Post(title: "iOS Development", author: "Apple", tag: Tag(name: "swift"))
let postDescription = firstPost.description()
Good Luck
Royce Pollard
24,267 PointsSteven Deutsch,
Yup, Makes sense. I get it now. I don't know what I was thinking. I need to focus up. Thanks again. Enjoy your dinner bro.
Royce Pollard
24,267 PointsRoyce Pollard
24,267 PointsThanks Steven Deutsch,
Do you mind just quickly elaborating on why the property in the struct Post does not work and I have to use dot syntax?
Steven Deutsch
21,046 PointsSteven Deutsch
21,046 PointsYes, my apologies. I was trying to do it quickly before dinner.
When you refer to tag, you are talking about the property inside of the Post struct that is of type Tag. This property is initialized with a specific instance of Tag. What you want to return in the string is not that instance of tag, but that instances name property. We use dot syntax to drill down into the Tag struct's name property from the Post's tag property.
Does this help?
Basically tag refers to the instance, where as tag.name refers to the name property of that instance.