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 trialRubens Neto
13,422 PointsBummer on code challenge.
Hi guys! My code works perfectly in Xcode but is bumming in code challenge. This is the error message: "Make sure you are declaring a method called description that returns a String"
This is my code:
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)"
}
}
let tag = Tag(name: "swift")
let firstPost = Post(title:"iOS Development", author:"Apple", tag: tag)
let postDescription = firstPost.description()
1 Answer
Jhoan Arango
14,575 PointsHey guys:
The compiler is working fine, you just missed one detail, which is hard to notice sometimes. When doing the return on the instance method, you want to use: tag.name
return "\(title) by \(author). Filed under \(tag.name)" // tag.name
This should fix your problem.
Complete Code
struct Tag {
let name: String
}
struct Post {
let title: String
let author: String
let tag: Tag
// Instance method
func description() -> String {
return "\(title) by \(author). Filed under \(tag.name)"
// tag.name will access the stored property of Tag.
}
}
// Creating an instance of Post & Tag
let firstPost = Post(title: "iOS Development", author: "Apple", tag: Tag(name:"swift"))
// Using the instance method and assigning it to a constant
let postDescription = firstPost.description()
Hope this helps
Steve Hunter
57,712 PointsHaha! Thank you - I was not seeing the wood for the trees!
Steve.
Rubens Neto
13,422 PointsThank you very much Jhoan Arango!!! It solved the problem. =)
Steve Hunter
57,712 PointsSteve Hunter
57,712 PointsCode looks OK to me - I did just try this and got the same result. There may be some glitches that need sorting out. It worked fine when I took the course! Give it some time and try again; it'll get fixed, I am sure. If not, email support with the issue.
One note about your code; your constructor for the
firstPost
constant can be done in one line without creating thetag
constant:let firstPost = Post(title:"iOS Development", author:"Apple", tag: Tag(name: "swift"))
Just a pointer! :-)
Steve.