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

Help Initializing Tag Struct inside of Post Struct. What am I doing Wrong?

We learned about instance methods before. For this task, add an instance method to Post.

Name the method description. It takes no parameters and returns a string that describes the post instance. For example given a title: "iOS Development", author: "Apple", and a tag named "swift", the description would read as follows"

"iOSDevelopment by Apple. Filed under swift"

Once you have an instance method, call it on the firstPost instance and assign the result to a constant named postDescription

structs.swift
struct Tag {
    let name: String
    }

struct Post{
    let title: String
    let author: String
    let tag: Tag
    init(title: String, author: String, tag: String){
        self.title = title
        self.author = author
        self.tag = Tag(name: tag)    }

    func description() ->String {

        return (" \(title) by \(author). Filled under \(tag) ")
    }

}

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

let postDescription = firstPost.description()
villa
villa
6,171 Points

You have tu call the name of the Tag. and on the init tag: is not type String . so first tag: Tag self.tag = tag and on your return value '''return "(title) by (author). Filed under (tag.name)'''

Thank you Juan.

12 Answers

Hi Ryan,

You don't need the create an init function. The code would look like:

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: "Treehouse blog", author: "S. Hunter", tag: Tag(name: "Swift, iOS"))
let postDescription = firstPost.description()

I like your code, that's a nice idea. But I don't think you can create the Tag like that. It isn't a string, so you have to pass a completed Tag instance into the Post.

And, as Juan has said, the name of the Tag instance needs accessing with dot notation in your interpolated string returned from description.

I hope that helps.

Steve.

Man Thank you! I was getting the error "Tag is not String", but I couldn't figure it our. This makes sense I needed to pass a completed Tag instance into the the Post. I have learned a ton from treehouse! Thanks Again.

Austin Money
Austin Money
2,304 Points

Correct me if i'm wrong, but i believe the title, author, and tag should all be variables and not constants... see if that helps.

Hi Ali,

The first task is to create a new struct called Post which has three stored properties, title and author are both of type String and the stored property tag is of type Tag which is a struct that is already defined for us. The Tag struct has one stored property, a String called name.

So, the Post struct would look like:

struct Post{
  let title: String
  let author: String
  let tag: Tag
}

That completes the struct. The next part of the challenge says, create an instance of Post and assign it to a constant named firstPost. Constants are created using the keyword let and it needs to be called firstPost. That starts:

let firstPost = 

Now we need to create the new instance of a Post. It takes two strings and a Tag. Let's start with the strings - they can be anything. the first is called title and the second is author:

let firstPost = Post(title: "A title", author: "An author" ...)

Lastly, it needs a Tag instance passing in. A Tag requires a name stored property to be created, which would look like:

let aTag = Tag(name: "A name")

But we're doing this inside the Post creation call. So we can just pass in the part to the right of the equals sign into our incomplete Post creation:

let firstPost = Post(title: "A title", author: "An author", tag: Tag(name: "A name"))

And that's it! I'll update this with the second part of the challenge in a minute.

Steve.

The second task requires a method adding to the struct. It is called description. It takes no parameters and returns a string. Inside, it just takes the three stored properties and interpolates them into a string which it returns.

So, the method skeleton looks like:

func description() -> String {

}

Inside, gather the stored properties into a string like "\(title) by \(author). Filed under \(tag.name)". The last one needs a little thought as you can't interpolate the tag; you want to use its name property. Add that string inside the method and return it:

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

Then you just create a constant and call the method on the firstPost instance:

let postDescription = firstPost.description()

And that's it

Steve.

Works pretty fine in playgrounds but still can't pass the challenge

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: "SomeTitle", author: "Some Guy", tag: Tag(name: "TAGTAG")).description()

let postDescription = firstPost

Hi there,

Your firstPost constant holds a string, not a Post because you have created a Post then called .description() on that post, which returns a string.

For firstPost to hold a Post remove the call to description:

let firstPost = Post(title: "SomeTitle", author: "Some Guy", tag: Tag(name: "TAGTAG"))

Then to get the description for your postDescription constant, call description on firstPost:

let postDescription = firstPost.description()

The challenge is expecting firstPost to hold an instance of Post but that's not what it's getting so the challenge is, correctly, failing.

Make sense?

Steve.

Hi Fiodar,

You need to pull out the name part of the Tag; you can't just put the Tag instance in there. So, in your description method, use tag.name rather than just tag.

Make sense?

Steve.

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)"
}
}
let firstPost = Post(title: "ios Development", author: "Apple", tag: Tag(name: "Swift"))
let postDescription = firstPost.description()

Something iam missing out? Keep getting an error.

Fiodar: You misspelled "iOSDevelopment"

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

[MOD: edited code block - srh]

Hi Mitchell,

The strings used by way of example within the challenge are just that, examples. You can use whatever values you want as long as the name element of the Tag instance gets assigned into postDescription, the challenge will pass. This works fine:

let firstPost = Post(title: "Title", author: "S Hunter", tag: Tag(name: "a tag"))
let postDescription = firstPost.description()

Make sense?

Steve.

struct Tag {
  let name: String
}

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

    init(title: String, author: String, tag: String){
        self.title = title
        self.author = author
        self.tag = Tag(name: tag)
    }

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

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

// I keep getting an error too

[MOD: edited code block - srh]

Hi Jun,

You are passing a String into the init method then creating a Tag instance inside that method. The challenge is expecting to be able to pass a Tag instance into the struct to create the instance.

First, delete the init method; you don't need it here. Then, where you create firstPost, pass a new Tag instance in, rather than a String. Something like:

let firstPost = Post(title: "A title", author: "An author", tag: Tag(name: "aTag"))

Let me know how you get on.

Steve.

ali aboutaleb
ali aboutaleb
4,025 Points

Steve i cant figure it out ive been trying... can you post the whole code and briefly explain it??

Give me a second - and do you have the link to the current challenge? This one has been retired as it is covering an old Swift version.

ali aboutaleb
ali aboutaleb
4,025 Points

Thank you steve. I dont know why its not compiling though.... do think its a problem in the compiler?

The challenges seem to be working here. Can you post your code?

Steve.

ali aboutaleb
ali aboutaleb
4,025 Points
struct Tag {
  let name: String
}

struct Post {
let title: String 
let author: String
let tag: Tag
}


let aTag = Tag(name: "A name")

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()

OK - you need to delete the let aTag line - that was just an example in my description, not part of this challenge.

Next, move the whole func inside the struct - that code is part of the Post struct. On a style point, consider your indents - having correctly indented code makes it much easier to read. Indeed, some languages require indents for the code to work, e.g. Python.

The rest looks fine.

Steve.

ali aboutaleb
ali aboutaleb
4,025 Points

Thanks steve your the best!

:+1: :+1:

//you can try this one, it works

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: "My First iOS App", author: "Rick Hanolt", tag: Tag(name: "Swift")) let postDescription = firstPost.description()