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

David Alberto Fuentes Ortiz
David Alberto Fuentes Ortiz
1,593 Points

build a simple iPhone app with swift 2.0 challenge 2 of 2 recap

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: "My App",author: "David O.", name: "My Ideas" )
let postDescription = firstPost.description()

5 Answers

Greg Kaleka
Greg Kaleka
39,021 Points

Hi David,

You're trying to represent a Tag object as a string, which you can't do. You need to use its name. Just change \(tag) to \(tag.name), and you should be good to go.


Edit: You've created an interesting initializer for Post (I'd want to use this as a convenience initializer, rather than default, but I like it), but the challenge isn't expecting anything but the default initializer, which is why it's throwing an (unhelpful) error. If you remove your initializer, you can create an instance like this:

let postExample = Post(title: "My Goald", author: "David Ortiz", tag: Tag(name: "D.O"))

Make sense?


Cheers :beers:

-Greg

Digvijay Jaiswal
Digvijay Jaiswal
5,565 Points
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)") //tag.name is for accessing the name

    }

}


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

let postDescription = firstPost.description()
David Alberto Fuentes Ortiz
David Alberto Fuentes Ortiz
1,593 Points

thank you for answering my question. it does work on my playground but not on the treehouse website it still giving me this error ------ Bummer! Your code could not be compiled. Please click on "Preview" to view the compiler errors

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.name)"

    }

}

let firstPost = Post(title: "My Goald", author: "David Ortiz", name: "D.O")

let postDescription = firstPost.description()
Greg Kaleka
Greg Kaleka
39,021 Points

Ah, I see the problem - should definitely not be a compiler error, but oh well. I've edited my answer above.

Digvijay Jaiswal
Digvijay Jaiswal
5,565 Points

David, You still have not called the instance of Tag object when you are calling it in the constant firstPost.

let firstPost = Post(title: "My Goald", author: "David Ortiz", tag: Tag(name: "D.O"))

Try now.

Cheerz! DJ

David Alberto Fuentes Ortiz
David Alberto Fuentes Ortiz
1,593 Points

thank you so much... I'm sorry for answering super late....