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 trialGuy Tam
3,221 PointsIssue assigning the result of a method to a constant
Hi,
I'm fairly certain that my code is correct, but may be missing something very obvious. I've tried a few different thing.
Does this look correct to you:
struct Tag { let name: String }
struct Post { let title: String let author: String let tag: Tag
func description() -> (String) {
let descriptionString = "\(self.title) by \(self.author). Filed under \(self.tag.name)"
return descriptionString
}
}
let firstPost = Post(title: "iOS Development", author: "Apple", tag: Tag(name: "swift"))
let postDescription = firstPost.description()
I keep getting the error "Bummer! Make sure you are calling the description method and assigning it to the postDescription constant"
Is there something I'm missing?
Thanks in advance!
struct Tag {
let name: String
}
struct Post {
let title: String
let author: String
let tag: Tag
func description() -> (String) {
let descriptionString = "\(self.title) by \(self.author). Filed under \(self.tag.name)"
return descriptionString
}
}
let firstPost = Post(title: "iOS Development", author: "Apple", tag: Tag(name: "swift"))
let postDescription = firstPost.description()
2 Answers
Jeff McDivitt
23,970 PointsThe first error you have is in your method. You do not need to declare a variable, just return a string
struct Tag {
let name: String
}
struct Post {
var title: String
var author: String
var tag: Tag
func description() -> String{
return "\(title) by \(author). Filed under \(tag.name)"
}
}
let firstPost = Post(title: "iOS Development", author: "Apple", tag: Tag(name: "Swift"))
let postDescription = firstPost.description()