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

I'm not sure why the code won't compile, suggestions would be great! From Swift recap part 1, build a simple iPhone app

This appears to compile just fine in Xcode but perhaps it's isn't what the challenge was looking for. I only get a "bummer" message stating that the code could not be compiled. Referring to "preview" for problems shows nothing but an empty screen. Any input would be helpful, thanks!

structs.swift
struct Tag {
    let name: String
}
struct Post {
    var title: String
    var author: String
    var 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: "hi", author: "cs", tag: "bye")
let postDescription = firstPost.description()
Nathan Tallack
Nathan Tallack
22,160 Points

Your code is good. Methinks there is something wrong with that challenge test script.

Ask a staffer to help you. :)

1 Answer

Steven Deutsch
Steven Deutsch
21,046 Points

Hey Colin Senfelds,

I think the challenge is being really specific in the way that it wants you to solve this. Instead of initializing an instance of tag inside of your Post struct, try doing it inside of the initializer call instead.

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

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

    func description() -> String {

        return "\(title) by \(author). Filed under \(tag.name)"
    }
}
let firstPost = Post(title: "hi", author: "cs", tag: Tag(name: "bye"))
let postDescription = firstPost.description()

Good Luck!

Nathan Tallack
Nathan Tallack
22,160 Points

Wow! Nicely spotted.

And a little bit crap for that challenge. If they want you to do that they should call it out explicitly in the challenge instrucitons.

I thought it would be worth mentioning that the reason I didn't want to write the code in the manner they preferred is because it seems like adding unnecessary code each time an instance of Post is created. If I can just write "Tag(name: tag)" once in the initializer, then I won't have to write "Tag(name: "someString)" each time I initialize a new instance, I can simply write the string by itself. Keeping the code dry! (: Thanks for both of your input!