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

Wojciech Pilwinski
Wojciech Pilwinski
1,723 Points

Error. XCode works OK, but ot on the teamtreehouse web

Hello. It's again me. And similar stupid problem. When I execute that code in xCode, everything is OK, but here, on teamtreehouse website, it gives me error:

"Your code could not be compiled. Please click on "Preview" to view the compiler errors."

Please help me, thanks in advance

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

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

}

let firstPost = Post(title: "Martin Eden", author: "Jack London", name: "aaa")
let postDescription: String = firstPost.description()
Martin Wildfeuer
Martin Wildfeuer
Courses Plus Student 11,071 Points

Have you clicked "Preview" to show the compiler errors? Could you please post what it says?

2 Answers

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

Hey there, ok I did the challenge just to see what code would pass and I ended up doing the following:

struct Tag {
    let name: String
}

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

    // Although you created a custom initializer in your code, for this assignment you
    // do not need it. I can see you wanted to create the tag within the initializer and only
    // pass the name for it, but for the sake of this assignment, let's use the initializer that
    // is automatically created for structs by Swift for you.

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

// Now we have to pass an instance of `Tag` to the initializer of `Post`, instead of a String
let firstPost = Post(title: "iOS Development", author: "Apple", tag: Tag(name: "swift"))
let postDescription = firstPost.description()

What you did worked fine and it could be the right approach as well. Code check on Treehouse does not cover all potential solutions, though, but mostly expects the most "straight forward" approach, though we could argue what that is :)

So, what code check most probably does, it tries to create an instance of Post with the auto-created initializer, which does not expect a String but a Tag. It does not know you did a custom implementation of the init method, therefore it tries to pass the Tag where a String is expected, thus fails...

Hope that helps and good job :)

P.S.

I have no clue why, but the forum editor htmlencodes the quotes when creating the instance of Post. Please bear with me :)

Wojciech Pilwinski
Wojciech Pilwinski
1,723 Points

Hello. Thank you for reply. But actualle I've just copied (few seconds ago) my own code from here, and pasted it to challenge, and now it works. So I don't know what problem it was. But I also need to tell you, that some time ago I stopped at all checking "Preview" because it always showed nothing, just black screen. But now I've just made intentionally mistake in code, just to see if now "Preview" works. And it works. So somebody just repaired that or there is something wrong with my browser. I use newest Chrome.

Wojciech Pilwinski
Wojciech Pilwinski
1,723 Points

Oh man!!! Sorry. It's still doesn't work. Actually I clicked "View challenge" and then pasted code. And it was ok, but actually after click "View challenge" it goes to first off two challenge. And that first challenge works perfect, but the problem is with second step. And also after click "Preview" it still shows nothing, just black screen. What can I do. Can you help me in any way? Thanks in advance