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 trialMichael Palmer
1,494 PointsRecap Question?? Build a Simple iPhone App with Swift 2.0
The task says:
"We learned about instance methods before. For this task, add an instance method to Post.
Name the method description. It takes no parameters and returns a string that describes the post instance. For example given a title: "iOS Development", author: "Apple", and a tag named "swift", the description would read as follows"
"iOSDevelopment by Apple. Filed under swift"
Once you have an instance method, call it on the firstPost instance and assign the result to a constant named postDescription"
I have the 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 firstPost = Post(title: "iOS Development", author: "Apple", tag: "Swift")
let postDecription = firstPost.description()
And on the second to last line I get the error "Cannot convert value of type 'String' to expected argument type 'Tag'.
So I change "let firstPost = Post(title: "iOS Development", author: "Apple", tag: "Swift")"
to now read: let firstPost = Post(title: "iOS Development", author: "Apple", tag: Tag(name: "Swift"))"
and it compiles correctly - but the output is this: "iOS Development by Apple. Filed under Tag(name: "Swift")"
Which doesn't work for answering this question.
Can anyone let me know where I'm going wrong please?
4 Answers
Scott Eremia-Roden
Courses Plus Student 15,623 PointsSo all you need to do for your fix is to change your syntax to "tag.name"
it will look like:
func description() -> String {
return "\(title) by \(author). Filed under \(tag.name)"
}
Michael Palmer
1,494 PointsNot sure why the post has formatted like that, hope you can read it!
Scott Eremia-Roden
Courses Plus Student 15,623 PointsNo problem. Yes you have to explicitly say what property you want to use. Hope that helps.
mazen qp
803 PointsWe learned about instance methods before. For this task, add an instance method to Post.
Name the method description. It takes no parameters and returns a string that describes the post instance. For example given a title: "iOS Development", author: "Apple", and a tag named "swift", the description would read as follows"
"iOSDevelopment by Apple. Filed under swift"
Once you have an instance method, call it on the firstPost instance and assign the result to a constant named postDescription
I don't know what to do please help
Michael Palmer
1,494 PointsMichael Palmer
1,494 PointsPerfect, thank you very much Scott! I didn't realise I had to specify that I was passing in .name if there was only one value in the structure I was accessing!
Thank you again. Michael