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 trialMika Izmylove
322 Pointshow to create an instance
My app crashes when trying to create an instance of Post and assign it to a constant named firstPost
Kevin Roundtree
4,575 PointsHi Mika,
Can you post your code?
Make sure your constant is of type Post if you want to assign an instance of Post to firstPost.
1 Answer
Steve Hunter
57,712 PointsHi Mika,
Here, you have created a struct called Post
which includes another struct called Tag
. You want to create an instance of Post
.
To do this, you need to give a value to all the stored properties inside Post
. That's further complicated by needing to create an instance of Tag
inside the creation of Post
!!
Have a look at this:
struct Tag {
let name: String
}
struct Post{
let title: String
let author: String
let tag: Tag
}
let firstPost = Post(title: "Title", author: "Me", tag: Tag(name: "Treehouse"))
Let me know if that makes sense. You add strings for the first two stored properties, then create a whole new instance of Tag
for the third.
Steve.
Kevin Roundtree
4,575 PointsKevin Roundtree
4,575 PointsCan you post your code?