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

Tobias Meier
Tobias Meier
6,017 Points

Challenge Task 2 of 2 Tag Post structs fails to compile. Works in XCode 7.3. Preview doesn't work either.

Anybody got an idea what the problem might be?

structs.swift
struct Tag {
    let name: String

//    init(name: String) {
//        self.name = name
//        print(name)
//    }
}


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.init(name: tag)
    }

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

}

let firstPost = Post(title: "first Post", author: "me", tag: "admin")

let postDescription = firstPost.description()

4 Answers

Anjali Pasupathy
Anjali Pasupathy
28,883 Points

Try using "Tag(name: tag)" rather than "Tag.init(name: tag)" in the init method of Post.

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

I hope this helps!

Tobias Meier
Tobias Meier
6,017 Points

// Still getting used to how this forum works. Deleted my previous post stating that importing Foundation solved the issue because it didn't. Just mistook passing the first step in the challenge for the second part. Unfortunately this seems to have deleted Anjali's second post as well.

Thank you for your help Anjali! I took your advice and changed Tag.init(name: tag) to Tag(name: tag). Didn't work either.

Preview does not work either and displays nothing.

I believe there is some difference between what works in XCode and what works in the browser code checking backend. Maybe some stricter ruleset to enforce good coding practices or to emphasize the importance of doing it a certain way.

Anjali Pasupathy
Anjali Pasupathy
28,883 Points

That is very strange. It should work with the solution I provided. Indeed, the compiler on the browser shows no errors when I run the code.

Here's another solution: In your init method, initialize tag with an object of type Tag, not String. Then, when you instantiate the post in firstPost, you can also instantiate a Tag object when you're inputting the tag parameter. This is a solution I've used before, and I tested it just now just in case.

    init(title: String, author: String, tag: Tag) {
        self.title = title
        self.author = author
        self.tag = tag // Use this instead
    }

// When initializing firstPost
let firstPost = Post(title: "first Post", author: "me", tag: Tag(name: "admin"))

I hope this actually helps this time! [x

Tobias Meier
Tobias Meier
6,017 Points

Thank you Anjali for your help! That did it.

I used tag: Tag and self.tag = tag in my first approach but ran into the type error upon initializing firstPost and resorted to using tag: String and calling Tag.init.

Anjali Pasupathy
Anjali Pasupathy
28,883 Points

You're welcome! I'm glad it worked! (: