Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

iOS Build a Simple iPhone App with Swift Getting Started with iOS Development Swift Recap Part 1

Deneen Edwards
Deneen Edwards
5,626 Points

Swift Recap Part 1 - Objective 2

I cannot get my code through the Code Challenge compiler for the 2nd task. Keeps saying Bummer. No errors in Previewer. My code compiled for Objective 1 and works in the Playground. But will not pass for Objective 2.

Does anyone see an error? I checked for spelling errors and brackets. For Task 2, I just added a method, and called tag.name for the name.

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

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

let firstPost = Post(title: "IOS Development", author: "Apple", name: "Swift")
firstPost.description()
let postDescription = firstPost.description()

3 Answers

Mitchell Richmond
Mitchell Richmond
5,401 Points

Hi Deneen, it might just not like the way you are doing it. A bit too complicated, here is how I did it:

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: "Harry Potter", author: "J.K", tag: Tag(name: "Hello"))

let postDescription = firstPost.description()
Deneen Edwards
Deneen Edwards
5,626 Points

Thanks Mitchell.

I made a new initializer because I would not ask a User to enter a structure. And because my code passed the 1st objective, I expected it to pass the 2nd if it didn't have errors and produced the correct result. But, I'll just change it to get the credit and move on.

Jenny Dogan
Jenny Dogan
4,595 Points

Hi Mitchell, can you explain this part of the function (tag.name)? why can't I just use (tag) Thanks!

Mitchell Richmond
Mitchell Richmond
5,401 Points

Hi Jenny! If you just put tag, it returns the entire Tag object which is not what we want. We just want the name, which is inside the Tag object. So when we put tag.name, it tells it to just print the name that the Tag object is storing. Does that make sense?