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 trialMatthew Phillips
Courses Plus Student 6,271 PointsCannot get challenge to pass
Hello,
I am trying to get the challenge to pass, (Second Step) but I am not having much look, I think I am missing something, but I have copied it into a xcode playground and it is not showing any errors, and shows the correct result.
Does anyone know where I am going wrong?
Thanks Matthew
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 tagging = Tag(name: "Swift")
let firstPost = Post(title: "iOSDevelopment", author: "Apple", tag:tagging)
let postDescription = firstPost.description()
3 Answers
Raul Martin
2,312 PointsYou are right Mattew, you only need to specity the init on a class. Its done automatically under the hood for Structs.
And yeah, the issues were only about spacing... I keep making those myself.
Cheers
Yusuf Akbar
2,457 PointsHello Matthew,
Great job so far, all seems to be correct. You are however missing the init methods for both structs.
This should help:
struct Tag { let name: String
init(name: String) {
self.name = name
}
}
struct Post { let title: String let author: String let tag: Tag
init(title: String, author: String, tag: Tag) {
self.title=title
self.author = author
self.tag = tag
}
func description() -> String {
return "\(title) by \(author). Filed under \(tag.name)"
}
} let swift = Tag(name: "swift") let firstPost = Post(title: "iOS Development", author: "Apple", tag: swift) let postDescription = firstPost.description()
Try pasting this into the challenge, it should pass.
Yusuf Akbar
2,457 PointsMy apologies for the formatting of the answer, not sure why it's splitting the code up.
Matthew Phillips
Courses Plus Student 6,271 PointsThanks Yusuf.
Just managed to work it out, I needed to remove a space on my func description() -> String, between description and ().
Thanks for your reply though. Much appreciated.
As a side question (i'm just learning Swift, so could be totally wrong) I thought Structs had a inferred Init if you do not specify one, and you only needed to specify a init on a class, is this not correct?
Yusuf Akbar
2,457 PointsAlways a pleasure.
I have also just started with Swift and when I got to this challenge, I too was a bit confused, still am actually. I am hoping that someone with a bit more experience can explain why :)