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 Architecture of an iOS App

Why will my code related to structures compile in my playground but not in the code challenge?

When i write the below code in Xcode the code it compiles perfectly fine, but when I try to complete the Recap Challenge part 2 of 2 from Build a Simple iPhone App with Swift 2.0 the code fails but no reason as to why is given. Could someone help me understand what I'm doing wrong? What specifically with my code is incorrect?

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: "Hello", author: "Adelle", tag: "18")
firstPost.description()

1 Answer

Just because your Xcode did compile something, it does not mean that you did the instructions of the challange in the right way.

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: "Hello", author: "Adelle", tag: Tag(name: "18"))
let postDescription = firstPost.description()

This is a common problem: People test the code in the IDEs and think everything is as it should be. But it does not mean anything to get no compile errors. You have to do what the instructions of the challange want from you.

For example: The error message of the challange already told you that something is wrong: Make sure you're calling description() on firstPost and assigning the results to a constant named postDescription