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 trialJared Feingold
316 PointsStruct within a struct
How exactly do you create a struct so that it contains another struct as one of its properties?
struct Tag {
let name: String
}
struct Post {
var title: String
var author: String
let tag: Tag()
}
let firstPost = Post(title: "hi", author: "Jeff")
2 Answers
Rogier Nitschelm
iOS Development Techdegree Student 5,461 PointsYou are almost there. When you specify a variable with a type you should put the type after the :
let tag: Tag = Tag(name: "some tag")
Or you could just let swift infer the type:
let tag = Tag(name: "some tag")
Scott Ho
288 Pointsstruct Tag { let name: String }
struct Post { var title: String var author: String let tag: Tag /<<<******/ }
let tag = Tag(name: "drop the ()") let firstPost = Post(title: "Hi", author: "Jeff", tag: tag)