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 trialPhilip Ladendorf
11,298 PointsCode is working in Xcode but compiler is not accepting
Hello, my code is working fine in Xcode and i cant find any bugs, but i always get the message of compiler errors! In preview mode is no error shown.
How can i solve this challenge?
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 {
let result = "\(title) by \(author). Filed under \(tag.name)"
return result
}
}
let firstPost = Post(title: "Wetter", author: "John Doe", name: "Test")
let postDescription = firstPost.description()
2 Answers
Greg Kaleka
39,021 PointsHi Phillip,
You've created an interesting initializer for Post (I'd want to use this as a convenience initializer, rather than default, but I like it), but the challenge isn't expecting anything but the default initializer, which is why it's throwing an (unhelpful) error. If you remove your initializer, you can create an instance like this:
let firstPost = Post(title: "Wetter", author: "John Doe", tag: Tag(name: "Test"))
Make sense?
Cheers
-Greg
Philip Ladendorf
11,298 PointsHello Greg,
thank you very much! I made this custom initializer because i thought its not possible to initialize the struct "tag" directly in the parameterlist of the Post - initializer.
Enjoy your weekend!
Philip
Greg Kaleka
39,021 PointsYeah I like it as a convenience initializer, but if you were building a real blog app, you'd often want to pass in existing tags. You'd probably have them already initialized themselves, something like this:
// somewhere earlier in your code
let testTag = Tag(name: "Test")
// somewhere later in your code
let firstPost = Post(title: "Wetter", author: "John Doe", tag: testTag)
Happy Saturday to you as well