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 2.0 Getting Started with iOS Development Swift Recap Part 1

shane reichefeld
PLUS
shane reichefeld
Courses Plus Student 2,232 Points

Can someone explain whats its asking and what each step is doing please

Help

structs.swift
struct Tag {
    let name: String
}
struct Post {
  let title: String
  let author: String
  let tag: Tag
}

let firstPost = Post(title: "X", author: "Y", tag: Tag(name: "swift"))

1 Answer

Hi Shane lets go bit by bit

struct Tag {
    let name: String
}

struct Post {
    var title: String
    var author: String
    var tag: Tag

// by initializing this Struct, you're making it so when you want to use the Struct properties, the title, author and tag. you need to add it in the init.

    init(title: String, author: String, tag: Tag) {
        self.title = title
        self.author = author
        self.tag = tag
    }
}

// Since the return type of Tag is another Struct names tag, you can see it takes in an parameter or input of a String. meaning you need to add a name. We need to first assign a constant or variable with a Tag struct input.
let testTag = Tag(name: "Fernando")
// After we just add the testTag constant as our Struct tag parameter
let firstPost = Post(title: "Hello", author: "D Shan", tag: testTag)