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 trialElijah Florence
Courses Plus Student 3,225 PointsWhy is my code not working
IT says make sure you are declaring an instance method named description that returns string. As far as I thought I had done this, but it is not going through
struct Tag {
let name: String
}
struct Post{
var title: String
var author: String
var tag: Tag
func decription() -> String{
var description1 = ("\(title) by \(author). Filed under \(tag)")
return(description1)
}
}
let firstTag = Tag(name: "Swift 2.0")
let firstPost = Post(title:"DinoRace", author: "Elijah Florence", tag: firstTag)
3 Answers
Stephen McMillan
iOS Development with Swift Techdegree Graduate 33,994 PointsHi Elijah,
A few mistakes here which is why the challenge isn't passing.
Firstly, you named it 'decription' instead of 'description'. :)
Next, in that method you need to use string interpolation which you have done correctly to an extent. Whilst you pass in name and author correctly you only passed in 'tag'. You actually need to say 'tag.name' to get the name string out of the firstTag constant. This name property comes from the struct Tag.
func description() -> String{
var description1 = "\(title) by \(author). Filed under \(tag.name)" // This is changed to tag.name
return(description1)
}
You also do not need the () around the string, instead you can just do = "".
The challenge also asks you to call the description function on firstPost and assign the value returned to a constant called postDescription. This is missing from your code above. You can do this like so below where you create an instance of Post.
let postDescription = firstPost.description()
These changes will pass for you.
However there is one other thing I'd like to say - you do not have to create a variable inside the function if you just want to return a value. Instead, you can just return the string like so.
return "\(title) by \(author). Filed under \(tag.name)"
Hope this helped.
Elijah Florence
Courses Plus Student 3,225 PointsThanks guys! I didn't even think about the fact that calling tag is calling the struct not just a variable from the struct. Glad I learned that now
Greg Kaleka
39,021 PointsNo worries - I've literally been asked to answer this question about a dozen times. The tag/tag.name trips up people A LOT. Happy coding!
Dirk Florence
10,891 PointsMispelled words often break my code.
- dad
Greg Kaleka
39,021 PointsGreg Kaleka
39,021 PointsStephen - I edited your last code block to have
tag.name
instead of justtag
to avoid confusion.Cheers
-Greg
Stephen McMillan
iOS Development with Swift Techdegree Graduate 33,994 PointsStephen McMillan
iOS Development with Swift Techdegree Graduate 33,994 PointsOops! My bad, thanks Greg! :)