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 trialJuan Carlos Torres
5,608 PointsHi, I just checked the following code at xCode 9 and swift 4 and it works fine, the page says I have errors.
The page shows a message saying that my code cannot be compiled, I run the same code at xCode and it works perfectly.
I don't know if this a bug, or maybe I'm doing something wrong.
Thanks for helping.
struct Tag {
let name: String
}
struct Post {
var title : String
var author : String
var tag : Tag
init(title: String, author: String, tag: String)
{
self.title = title
self.author = author
self.tag = Tag(name : tag)
}
func description () -> String {
return "\(title) by \(author). Filed under \(tag.name)"
}
}
let firstPost = Post(title : "iOS Development", author: "Apple", tag: "swift")
let postDescription = firstPost.description()
3 Answers
Greg Kaleka
39,021 PointsHi Juan Carlos,
I think this might be a bug in the challenge. What you're doing in your code is perfectly valid as far as I can see. The reason the code challenge is failing is that it wants you to pass a Tag
instance into the init method, rather than a string that you then use to create the tag. Frankly... I like your implementation, at least as a convenience initializer. I think it makes sense to have an initializer that takes a tag instance (like the challenge wants), since in a real application, you would probably create tags separately, but that's just my opinion. There's certainly a case to be made that you would want to create tags on the fly (maybe adding some additional logic to see if the tag exists before creating it, but still).
Paging Pasan Premaratne: am I missing any reason why Juan Carlos's code shouldn't pass?
For now, here's how to make the challenge happy:
struct Tag {
let name: String
}
struct Post {
var title : String
var author : String
var tag : Tag
init(title: String, author: String, tag: Tag) // use a Tag instance as a parameter
{
self.title = title
self.author = author
self.tag = tag // no initialization needed here
}
func description () -> String {
return "\(title) by \(author). Filed under \(tag.name)"
}
}
// I'm creating the tag inline. Alternatively, you could create a constant and then pass that in.
let firstPost = Post(title : "iOS Development", author: "Apple", tag: Tag(name: "swift"))
let postDescription = firstPost.description()
Hope this makes sense!
Cheers
-Greg
stjarnan
Front End Web Development Techdegree Graduate 56,488 PointsHi Juan,
Sometimes there are mutliple ways of doing something using code, but the instructions only allow you to use one of them.
My advice is that you read through the instructions for the challenge again and make sure you're doing it that way.
Jonas
Juan Carlos Torres
5,608 PointsThank you!