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 trialChris White
2,308 PointsGetting "compiler errors" and the preview function crashes but my code works perfectly in Xcode
I'm not sure if I'm missing something or whether there is a bug in Treehouse's compiler, I have the code working in my Xcode Swift Playground and returning the string as specified, however, when I try to check my work I get a compiler error that then asks me to use the preview for more information. When I click preview code checker window shakes side-to-side to, flashes white and then remains blank.
I wrote support to report a possible bug but their response was that they aren't programmers and can't help with this.
Did I do something wrong or is this a bug on Treehouse's end?
struct Tag {
let name: String
}
struct Post {
let title: String
let author: String
let 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()
1 Answer
Jhoan Arango
14,575 PointsHello Chris,
Your code is perfect, no doubt about that. The problem is that the treehouse compiler is a bit picky. You created your own initializer for the struct and the compiler or the challenge didn't ask you to create one, so it may be expecting you to use the memberwise initializer that comes standard in all structs.
So try this:
struct Tag {
let name: String
}
struct Post {
var title: String
var author: String
var tag: Tag
func description() -> String {
return "\(title) by \(author). Filed under \(tag.name)"
}
}
let firstPost = Post(title: "iOSDevelopment", author: "Apple", tag: Tag(name: "swift"))
let postDescription = firstPost.description()
that should do the trick.
Good luck hope that helps
Chris White
2,308 PointsChris White
2,308 PointsAh ha, thank you! I had tried using memberwise initialization originally but kept getting a type error when passing a string and I didn't understand how to pass a proper
Tag
in though now seeing how you did it makes complete sense.I do wish the compiler could have been more helpful and the crashing behavior was really odd, is there any way to flag this for their dev team?