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 trialAbdijabar Mohamed
1,462 PointsWhat is missing in my code?
// I HAVE BEEN STRUGGLING WITH THIS QUESTION SINCE LAST YEAR!!! PLEASE HELP
struct Tag {
let name: String
}
struct Post {
let title: String
let author: String
let tag: Tag
func description() -> String {
return "\(title) by \(author). Filled under \(tag.name)"
}
}
let firstPost = Post(title: "iOS Development", author: "Apple", tag: Tag(name: "Swift"))
let postDescription = firstPost.description()
// I wanna return "iOS Development by Apple. Filled under Swift"
[MOD: added code markup}
Thanks and welcome to 2016!
4 Answers
Nathan Tallack
22,160 PointsYou were VERY close. You had one typo (filled instead of filed) and you needed to escape the interpolated values (putting a backslash before the open brackets). See your fixed code below.
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)" // Typo here and missing backslashes.
}
}
let firstPost = Post(title: "iOS Development",
author: "Apple",
tag: Tag(name: "Swift")
)
let postDescription = firstPost.description()
Keep up the great work! :)
Steve Hunter
57,712 PointsHiya,
In the interpolation, tag
references the whole struct
while tag.name
accesses the stored property you want to display.
Steve.
Abdijabar Mohamed
1,462 PointsI put in tag.name but what is the difference between tag.name and tag in the interpolation?
Abdijabar Mohamed
1,462 PointsI put in tag.name but what is the difference between tag.name and tag in the interpolation?
Nathan Tallack
22,160 PointsAs Steve said below, tag.name accesses the stored property. Remember your object firstPost has properties (both value types and computed types). In this case firstPost.tag is set to Tag.name which is a stored property of the type String.
So when you are calling the description method on your firstPost object it is returning a String which you are assigning to the postDescription value. That string is being defined using both the literal values inside of the quotation marks and the interpolated values within it which are inside the escaped brackets.