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

Instance for structure

Got the type String, but cannot put a variable under "tag". It doesn't say in instructions what type it is. Nothing works!

Challenge Task 1 of 2

In the editor you've been provided with a Tag type. Create a struct named Post with three stored properties: title of type String author of type String tag of type Tag

Create an instance of Post and assign it to a constant

named/Users/mattconway/Desktop/Screen Shot 2016-01-28 at 5.40.31 PM.png firstPost.

structs.swift
struct Tag {
    let name: String
}
struct Post {
  let title: String
  let author: String
  let tag: Tag
}
let firstPost = Post(title: "Treehouse", author: "Pasan", tag: x)

5 Answers

Matthew Spear
Matthew Spear
14,534 Points

You need to construct an instance of the tag in the same way in which you construct the Post:

let tagX = Tag(name: "Tag X")

That variable can then be passed into the Post instance since the constant tagX is of type Tag.

Hope that helps, Matt

Matthew Spear
Matthew Spear
14,534 Points

Look at this after you've attempted it (more likely to learn that way):

So there are two options here either passing in a variable of type Tag (1) or creating a Tag instance whilst creating the Post Struct (2):

(1):

let tagX = Tag(name: "Tag X")
let firstPost = Post(title: "Treehouse", author: "Pasan", tag: tagX)

(2):

let firstPost = Post(title: "Treehouse", author: "Pasan", tag: Tag(name: "Tag X"))

Either are of which are valid!

Hey Matt Conway, Not sure if this has been answered correctly for you yet but this worked for me............

struct Tag { let name: String } struct Post { let title: String let author: String let tag: Tag func description() -> String { return "(title) by (author). Filed under (tag.name)" } } let firstPost = Post(title: "iOSDevelopment", author: "Apple", tag: Tag(name: "Swift")) let postDescription = firstPost.description()

alex banaga
alex banaga
310 Points

Thank you for this!

Jialong Zhang
Jialong Zhang
9,821 Points
struct Tag {
    let name: String
}

struct Post{
    let title: String
    let author: String
    let tag: Tag
}
let firstPost = Post(title: "Bird", author: "May", tag: Tag(name: "tag"))
Lydia Chandrahirawati
Lydia Chandrahirawati
6,336 Points
struct Tag {
      let name: String
}

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

    func description() -> String {
        return("\(title) by \(author). Filed under \(tag.name)")
    }
}

let firstPost = Post(title: "iOS Development", author: "Apple", tag: Tag(name: "swift"))

let postDescription = firstPost.description()

struct Tag { let name: String }

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

func description() -> String {
    return("\(title) by \(author). Filed under \(tag.name)")
}

}

let firstPost = Post(title: "iOS Development", author: "Apple", tag: Tag(name: "swift"))

let postDescription = firstPost.description()