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 trialDarcy Phillips
4,087 PointsCode could not be be compiled, but why?
This code seems to run fine in my playground, but when I paste it into the editor it says it can not be compiled. I don't understand why- I think I've done everything right?
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
tag = Tag(name: name)
}
func description() -> String {
return "\(title) by \(author). Filed under \(tag.name)"
}
}
let firstPost = Post(title: "iOSDevelopment", author: "Apple", name: "swift")
let postDescription = firstPost.description()
2 Answers
Anjali Pasupathy
28,883 PointsYou haven't done anything wrong. The challenge just expects the initializer to have a Tag object rather than the String name in its initializer. Because it doesn't see that, it says there's an error, even though there's nothing wrong with your code.
To pass the challenge, you need to pass a Tag object rather than the name in your init method:
init(title: String, author: String, tag: Tag) {
self.title = title
self.author = author
self.tag = tag
}
You also need to instantiate firstPost with a Tag rather than a name String:
let firstPost = Post(title: "iOSDevelopment", author: "Apple", tag: Tag(name:"swift"))
I hope this helps!
Darcy Phillips
4,087 PointsOh, I see! I didn't realize you could run the init method of the struct inside the instantiation procedure. That's cool. Thank you!
(it's a bit frustrating, every time I go back to the challenge, to have to copy and paste my way through the first step, which I've already passed, to try the second step again. Had to do it about 6 times!)
Anjali Pasupathy
28,883 PointsYep. You need to run the init method every time you instantiate something - otherwise, the instantiation wouldn't know what the values of its properties are, and the init method would serve no purpose. (:
(I know what you mean. It gets even more frustrating when the challenge stops responding and has to restart! I've gotten into the habit of copying all my code before I check it.)
I'm glad I could help!
Darcy Phillips
4,087 PointsDarcy Phillips
4,087 PointsI did update it to be slightly more readable- (in init, changed "name" to "tagName", and of course changed all the subsequent references). But it should be identical to the compiler. (I tried to paste the new code here, but it was ugly, so I deleted).