Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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

Not sure what the issue is.

Can someone please tell me what's going on?

structs.swift
struct Tag {
    let name: String
}

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

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

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

1 Answer

Martin Wildfeuer
PLUS
Martin Wildfeuer
Courses Plus Student 11,071 Points

There is one tiny thing in the description: replace tag with tag.name so it will not output the Tag struct, but the name property of Tag.

return "\(title) by \(author). Filed under \(tag.name)"

Although your code works perfectly, code check expects the tag being passed to the initializer, instead of it being created in a custom initializer. By doing that, you can get rid of your custom initializer and use the default one created for you.

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)"
    }
}

// Pass a Tag instance rather than a string
let firstPost = Post(title: "iOSDevelopment", author: "Apple", tag: Tag(name: "swift"))
let postDescription = firstPost.description()

I have to say that this is not obvious, even more so your code triggers a compiler error in code check but no output. The reason is that code check tries to test your initializer by passing a Tag where you are expecting a string name instead.

Hope that helps :)

It makes sense but it is still not working...just posted another question.