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 trialAlex Nelsen
Courses Plus Student 2,977 PointsI need help understanding instances and assignments.
I am struggling with terminology and syntax.
Please explain an instance and then how to assign that instance.
struct Tag {
let name: String
}
struct Post {
let title: String
let Author: String
let tag: Tag
}
3 Answers
Michael Reining
10,101 PointsHi Alex,
I know what you mean. This was also very confusing for me at first. However, once you get the hang of it, it is very simple.
In this code challenge we have two types of Objects. The Post and the Tag. The Post struct is like the blueprint. It describes what a Post object should look like. An instance is just any particular post. If you think about a blog, it consists of many posts. Each unique post is an instance of the Post struct.
Assignment is when you create a new variable and declare what it is.
Here is some code that explains this.
struct Tag {
let name: String
}
struct Post {
let title: String
let author: String
let tag: Tag
}
// first instance of Post assigned to a variable called firstPost
let firstPost = Post(title: "Post 1 Title", author: "author name", tag: Tag(name: "random"))
// second instance of Post assigned to a variable called secondPost
let secondPost = Post(title: "Post 2 Title", author: "author name", tag: Tag(name: "super"))
I hope that helps,
Mike
PS: If you found the above helpful, be sure to check out my app which breaks code challenges down and really explains things so you can learn faster.
Code! Learn how to program with Swift
https://itunes.apple.com/app/code!-learn-how-to-program/id1032546737?mt=8
PPS: I could not have developed the above app without going through the awesome resources on Team Treehouse. Stick with it. It is so worth it!
Alex Nelsen
Courses Plus Student 2,977 PointsThank you for the help. I got close to this as I tried more, but this explains why.
Alex
Alex Nelsen
Courses Plus Student 2,977 PointsI like the app!