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

Leslie Borrell
Leslie Borrell
2,318 Points

showing errors in tree house but not in xcode. no compile errors are showing

struct Post {
    let title: String
    let author: String
    let 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.name)"
    }
}

let firstPost = Post.init(title: "Leslie", author: "Borrell", name: "Fiction")

let postDescription = firstPost.description()
structs.swift
struct Tag {
    let name: String
}

struct Post {
    let title: String
    let author: String
    let 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.name)"
    }
}

let firstPost = Post.init(title: "Leslie", author: "Borrell", name: "Fiction")

let postDescription = firstPost.description()

1 Answer

Greg Kaleka
Greg Kaleka
39,021 Points

Hi Leslie,

A couple of things about your code:

  1. For a Struct, there's no need for an initializer if you're only setting self.property = property. This comes for free with Structs
  2. You don't need to call the init method directly. Instead of Tag.init(...), you can simply call Tag(...)
  3. Your third argument when you initialize Post is a String, but should be a Tag.

This should solve the issue:

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: "Leslie", author: "Borrell", tag: Tag(name: "Fiction"))

let postDescription = firstPost.description()
Leslie Borrell
Leslie Borrell
2,318 Points

Hi, Thanks for the response. I understand the the init is not required, but is there a reason that causes the code to fail? I updated the code per your suggestion, but couldn't get it to pass until i deleted the init function.

Thanks, Leslie