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 trial

iOS Build a Simple iPhone App with Swift Getting Started with iOS Development Swift Recap Part 1

Jared Feingold
Jared Feingold
316 Points

Struct within a struct

How exactly do you create a struct so that it contains another struct as one of its properties?

structs.swift
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
seal-mask
.a{fill-rule:evenodd;}techdegree
Rogier Nitschelm
iOS Development Techdegree Student 5,461 Points

You 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
Scott Ho
288 Points

struct 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)