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 trialDeneen Edwards
5,626 PointsSwift Recap Part 1 - Objective 2
I cannot get my code through the Code Challenge compiler for the 2nd task. Keeps saying Bummer. No errors in Previewer. My code compiled for Objective 1 and works in the Playground. But will not pass for Objective 2.
Does anyone see an error? I checked for spelling errors and brackets. For Task 2, I just added a method, and called tag.name for the name.
struct Tag {
let name: String
}
struct Post {
let title: String
let author: String
let tag: Tag
init (title: String, author: String, name: String) {
self.title = title
self.author = author
self.tag = Tag(name:name)
}
func description() -> String {
return "\(title) by \(author). Filed under \(tag.name)"
}
}
let firstPost = Post(title: "IOS Development", author: "Apple", name: "Swift")
firstPost.description()
let postDescription = firstPost.description()
3 Answers
Mitchell Richmond
5,401 PointsHi Deneen, it might just not like the way you are doing it. A bit too complicated, here is how I did it:
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)"
}
}
let firstPost = Post(title: "Harry Potter", author: "J.K", tag: Tag(name: "Hello"))
let postDescription = firstPost.description()
Jenny Dogan
4,595 PointsHi Mitchell, can you explain this part of the function (tag.name)? why can't I just use (tag) Thanks!
Mitchell Richmond
5,401 PointsHi Jenny! If you just put tag, it returns the entire Tag object which is not what we want. We just want the name, which is inside the Tag object. So when we put tag.name, it tells it to just print the name that the Tag object is storing. Does that make sense?
Deneen Edwards
5,626 PointsDeneen Edwards
5,626 PointsThanks Mitchell.
I made a new initializer because I would not ask a User to enter a structure. And because my code passed the 1st objective, I expected it to pass the 2nd if it didn't have errors and produced the correct result. But, I'll just change it to get the credit and move on.