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 trialCarl Smith
8,185 PointsNot sure how I can pass the Tag into another structure and make it work with an instance?
Not sure how I can pass the Tag into another structure and make it work with an instance?
struct Tag {
let name: String
}
struct Post {
var title: String
var author: String
var tag: Tag
}
let firstPost = Post(title: "title", author: "author", tag: "name")
2 Answers
jcorum
71,830 PointsThe first step is to create the member variables, which you did. Then you need to create the function the challenge asked for: description(), which takes no parameters, hence the empty parentheses, but does return a String, hence the -> String part. Inside the function you need to write a statement that uses String interpolation and returns that statement when the description() method is called.
struct Post {
let title: String
let author: String
let tag: Tag
func description() -> String {
return "\(title) by \(author). Filed under \(tag.name)"
}
}
let tag = Tag(name: "swift")
let firstPost = Post(title: "iOS Development", author: "Apple", tag: tag)
let postDescription = firstPost.description()
Note that since the tag member property is of type Tag, you need to create a Tag object, and then use that object when creating a Post object (firstPost).
jcorum
71,830 PointsThere's a problem with that challenge. The apostrophe in I'm makes everything you enter look like a comment. But the editor will still take your work. However, it's nicer when you remove it. Re the challenge, here's some help if you want it (spoiler alert!!):
class Robot: Machine {
override func move (direction: String) {
switch direction {
case "Up":
self.location.y++
case "Down":
self.location.y--
case "Left":
self.location.x--
case "Right":
self.location.x++
default: break
}
}
}
Carl Smith
8,185 PointsCarl Smith
8,185 PointsThank you! I actually figured it out and am now stuck on the next challenge trying to override a function..