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

Matthew McCurdy
Matthew McCurdy
2,276 Points

Is this swift code challenge buggy or am I doing something wrong?

I don't understand why I should have to create an instance of Tag before creating a post when I can write it like this instead (treehouse does not accept this answer):

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: "iOSDevelopment", author: "Apple", tag: "swift")

let postDescription = firstPost.description()

Treehouse does accept this answer:

structs.swift
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 aTag = Tag(name: "swift")

let firstPost = Post(title: "iOSDevelopment", author: "Apple", tag: aTag)

let postDescription = firstPost.description()

2 Answers

Matthew, good question. Your code works in an Xcode playground, so you expect it to work in Treehouse's challenge environment. But that assumes that Treehouse spent the time to replicate most of playground's code, which would be quite an undertaking. You may have noticed that they don't offer a Workspace for this course. Too much work, especially when folk would need Xcode anyway. So presumably the challenge editor is just looking for certain things, like, for example, an explicit Tag object.

Matthew McCurdy
Matthew McCurdy
2,276 Points

Gotcha. I've never taken any of their other tracks, so I'm not sure how they vary (implementation of a "Workspace," as you mention).

Does this mean that the code I supplied up top is a correct way of solving the problem even though it wasn't accepted? It's fine if the editor is looking for certain things, I just want to make sure I am not missing something important in the concepts which makes my code up top wrong.

Well, if it works in Xcode you can feel pretty comfortable about it!