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

PostDescription

Still having issue with this code.

structs.swift
struct Tag {
    let name: String
}
struct Post {
    let title: String
    let author: String
    let tag: Tag
    init (title: String, author: String) {
        self.title = title
        self.author = author
        self.tag = Tag(name: "Swift")
            }
    func description() -> String {
                let p1 = "\(title) by \(author). Filed under \(self.tag.name)"
        return p1

    }
}

let firstPost = Post(title: "iOSDevelopment", author: "Apple")

let postDescription = firstPost.description()

2 Answers

David Lin
David Lin
35,864 Points

Also, since Post is a struct, the intializer is automatically created for you, so you don't even need to write your own init() method for it.

Simon Di Giovanni
Simon Di Giovanni
8,429 Points

Hello Ciaran

Ok - the reason you're receiving an error, is because you're not initialising the constant 'tag' in your init method to accept a passed in value, you're assigning it a value in the init method.

Please take a look at the below example, which worked for me.

struct Tag {
    let name: String
}
struct Post {
    let title: String
    let author: String
    let tag: Tag
    init (title: String, author: String, tag: Tag) {
        self.title = title
        self.author = author
        self.tag = Tag(name: tag.name)
    }
    func description() -> String {
        let p1 = "\(title) by \(author). Filed under \(tag.name)"
        return p1

    }
}

let firstPost = Post(title: "iOSDevelopment", author: "Apple", tag: Tag(name: "Swift"))

let postDescription = firstPost.description()

So first take a look at the line

    init (title: String, author: String, tag: Tag)

I've told the init method to accept a another parameter, of type Tag.

Next -

self.tag = Tag(name: tag.name)

This tells the init method to set up 'tag' to accept the struct Tag.

let firstPost = Post(title: "iOSDevelopment", author: "Apple", tag: Tag(name: "Swift"))

You'll notice that in firstPost you have to type out the correct syntax for initialising the Tag struct, and you put "Swift" into the place of tag.name, which then allows you to create a full instance of Post.

I hope this helps. Please let me know if you don't understand what I've written.

Regards

Simon