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

struct method calls

I can't get this to output only "swift". Any suggestions?

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: "swift")
    }
    func description() -> String {
        return "\(title) by \(author).  Filed under \(Tag(name: "swift"))"
    }
}

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

4 Answers

Hey Martin Wildfeuer =) sure thing. I added some comments. Hope it got better.

Yep! Thanks

Perfect! Thank you very much :)

Hey Joe,

Try this:

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: "swift")
    }
    func description() -> String {
        return "\(title) by \(author).  Filed under \(tag.name))" // doing this way you will print only the value of name that is 'swift'
    }
}

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

let postDescription = firstPost.description()

Thanks. That works in my playground, but for some reason the code challenge tells me it is incorrect. Here is a link to the code challenge. https://teamtreehouse.com/library/build-a-simple-iphone-app-with-swift-20/getting-started-with-ios-development/swift-recap-part-1

Ahhh

Try this one then:

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 // Your code was not working because instead of assigning the value tag that you receive in the parameters you were instead creating another Tag. When you created an instance of Post, you were already creating a Tag and passing it as an argument so you did not need to do it again.
    }
    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()

Hey Tassia Castro! Thanks for your answer. Would you mind explaining what you changed and why that works compared to your last answer? Code only is a bit like cheating ;)

Thanks again, but the code challenge still doesn't like it.... hmmm

Ohh, it's just a space that was causing the error now..

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
    }
    func description() -> String {
        return "\(title) by \(author). Filed under \(tag.name)" // just a space between the period and the word Filed that shouldn't be there =)
    }
}

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

let postDescription = firstPost.description()