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 trialDerek Scollon
8,422 PointsWhat's wrong with this instance method?
No matter how hard I look, I can't see why this code isn't passing the code challenge. When I put it into an XCode playground, it gives me the result I expect.
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 myTag = Tag(name: "swift")
let firstPost = Post(title: "iOSDevelopment", author: "Apple", tag: myTag)
let postDescription = firstPost.description()
1 Answer
Greg Kaleka
39,021 PointsHi Derek,
This is a simple spacing issue. You should have equal spacing before and after the return operator:
// func description() ->String {
func description() -> String {
I don't think this would cause a compile error, but it's a good style habit to get into. I think the code challenges use SwiftLint, which enforces good style. Check out the GitHub repo.
As an aside, sloppy spacing is something that drives me crazy about Gabe Nadel's videos. He is super-inconsistent with his spacing, and maybe it's just my OCD, but it kills me.
Cheers
-Greg
Derek Scollon
8,422 PointsDerek Scollon
8,422 PointsMany thanks, that fixed it and gave me a good tip too!
Greg Kaleka
39,021 PointsGreg Kaleka
39,021 PointsSure thing! One extra tip: incorrect spacing will cause a compile error for assignment. Type the following into a playground:
The first two will throw errors, while the last two will not. I very strongly prefer the last one, and that's how you'll see it done almost always. It's how you're doing it already, so that's good .