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 trialBrian Murray
1,564 PointsBuild a Simple iPhone App Code Challenge:Swift Recap Part 1
The following code works in playground but I get an error "... declaring an instance method named description that returns a String.
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 firstPost = Post(title: "iOS", author: "Apple", tag: Tag(name: "swift"))
let postDescription = firstPost.description()
Brendan Whiting
Front End Web Development Techdegree Graduate 84,738 PointsWeird. I guess leaving out the space is standard practice, but why would the code challenge not compile? Maybe they're checking for it in by searching your code for the text "description()" rather than testing the code by its function.
3 Answers
Marina Alenskaja
9,320 PointsI don't understand - I have the exact same code, but it gives me an error..
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 firstPost = Post(title: "Holiday in Bali", author: "alenskaja", tag: Tag(name: "Holiday"))
let postDescription = firstPost.description()
Brendan Whiting
Front End Web Development Techdegree Graduate 84,738 PointsThere should be no comma in this line:
return ("\(title), by \(author). Filed under \(tag.name)")
Marina Alenskaja
9,320 PointsAaaah thank you so much, I didn't catch that :-)
Ashleigh Nombre
2,308 PointsI still didn't quite get the question but Im going to go back and learn what I missed in the question I didn't realize you need to create a function in order to get the right answer.
Brian Murray
1,564 PointsBrian Murray
1,564 PointsFound the problem. I had a space between "description" and the "()"